zoukankan      html  css  js  c++  java
  • Android Service 启动和停止服务

    activity_main.xml

      定义两个Button控件,start_servicestop_service

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/activity_main"
        tools:context=".MainActivity">
    
       <Button
           android:id="@+id/start_service"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="Start Service"/>
    
        <Button
            android:id="@+id/stop_service"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Stop Service"/>
    </LinearLayout>

    MainActivity.java

       同样定义两个Button控件start_servicestop_service,并设置监听器,然后用Intent对象进行通信。

    package jiangbin.servicetest;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends Activity implements View.OnClickListener{
    
        private Button startService;
        private Button stopService;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            startService = (Button) findViewById(R.id.start_service);
            stopService = (Button) findViewById(R.id.stop_service);
            startService.setOnClickListener(this);
            stopService.setOnClickListener(this);
        }
    
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.start_service:
                    Intent startIntent = new Intent(this, MyService.class);
                    startService(startIntent);
                    break;
                case R.id.stop_service:
                    Intent stopIntent = new Intent(this, MyService.class);
                    stopService(stopIntent);
                default:
                    break;
            }
        }
    }

    MyService.java

      定义Myservice

        onCreate()在服务创建时候调用;

        onStartCommand()在服务创建的时候调用;

        onDestory()在服务销毁的时候调用。

    package jiangbin.servicetest;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.util.Log;
    
    public class MyService extends Service {
        @Override
        public IBinder onBind(Intent intent){
            return null;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            Log.d("Myservice", "onCreate");
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.d("Myservice", "onStart");
            return super.onStartCommand(intent, flags, startId);
    
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            Log.d("Myservice", "onDestory");
        }
    }

            

  • 相关阅读:
    java初级应用------>判断某个字符串是否满足一定的格式要求------>使用正则表达式判断是否满足格式要求
    java基础---->数据结构与算法---->Java API:集合 以及 排序相关API
    Spring Framework------>version4.3.5.RELAESE----->应用实例------->测试spring framework的IOC功能
    Spring MVC------->version4.3.6-------->知识点------>实现Controller(编程思路)
    Spring MVC------->version4.3.6-------->知识点------>DispatchServlet和它对应的WebApplicationContext
    Spring MVC------->version4.3.6-------->概述
    数据结构与算法----->数据结构----->树------->二叉树的遍历
    多态 / 面向接口编程
    sqlserver存储过程的使用
    sqlserver数据库中sql的使用
  • 原文地址:https://www.cnblogs.com/danbing/p/5020624.html
Copyright © 2011-2022 走看看