• 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");
        }
    }

            

  • 相关阅读:
    9011,9012,9013,9014,8050,8550三极管的区别
    XP制动关机CMD命令
    搭建系统框架发现的三个Web.Config问题
    监听公众号返回按钮,直接退出到公众号页面
    微信公众号h5页面自定义分享
    博客园页面设置
    js 中加减乘除 比较精确的算法,js本身有些运算会出错,这里给出较精确的算法
    HTML属性
    HTML属性
    处理ajax数据;数据渲染(细节)
  • 原文地址:https://www.cnblogs.com/danbing/p/5020624.html
Copyright © 2020-2023  润新知