23 服务的创建和 安卓6.0版本注意点

API23 以上发送服务需要在意图指定应用包名

getPackageName()方法获取的是你所在应用的包名。不是你当前类所在的包

创建

  • 步骤一:
    创建一个类继承Service
package com.fmy.bokezhuanyong.server;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class MyServer extends Service{

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
    }

    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }

    /**
     * 通过启动服务 startService() 调用此方法
     * 参数1:启动时传递的对象
     * 参数2:flags  标记
     * 参数3:表示请求的标识
     * 
     * 返回值:
     * START_STICKY:粘性的Service    表示被kill掉  尝试重新去启动  如果服务启动成功会调用此方法    intent 的值null
     * START_NOT_STICKY:非粘性Service   表示被kill掉只能通过StartService()再次启动
     * START_REDELIVER_INTENT:表示被Kill掉  系统自动重启改服务  并将Intent传入
     *
     *
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 一般使用
            return START_NOT_STICKY;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        // TODO Auto-generated method stub
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }



}
  • 步骤二:
    清单文件注册
 <service android:name="com.fmy.bokezhuanyong.server.MyServer">
            <intent-filter>
                <action android:name="yitu.yitu"></action>
            </intent-filter>
        </service>

启动服务

  • 方式一:
    //方法一 显示跳转
        //Intent intent = new Intent(this, MyServer.class);
        //startService(intent);

        //方法一
        Intent intent = new Intent("yitu.yitu");
        //6.0高版本适配 填写的是应用的包名 不是服务所在的包!!!!!
        intent.setPackage("com.fmy.bokezhuanyong");
        startService(intent);

方式一生命周期:
1. onCreate()
2. onStartCommand()
3. onDestroy()

  • 方式二绑定启动:

    • 服务在本应用中:
      1 在服务中创一个类继承Binder并且内部写一个返回服务对象的方法 即可调用服务内的方法
      2 onBind(Intent intent) 返回一个继承Binder的对象

      @Override
          public IBinder onBind(Intent intent) {
      
              return new MyIbinder();
          }
      
          class MyIbinder extends Binder{
              public MyServer getServer(){
                  return MyServer.this;
              }
          }

      3 在启动服务的类
      创建类继承ServiceConnection

    package com.fmy.bokezhuanyong;
    
    import com.fmy.bokezhuanyong.server.MyServer;
    import com.fmy.bokezhuanyong.server.MyServer.Mybinder;
    
    import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.os.Bundle;
    import android.os.IBinder;
    
    public class MainActivity extends Activity {
    
        private Intent intent;
        private Myconn myConn;
        MyServer myServer;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            intent = new Intent("yitu.yitu");
            //6.0高版本适配 填写的是应用的包名 不是服务所在的包!!!!!
            intent.setPackage("com.fmy.bokezhuanyong");
            myConn = new Myconn();
    
        }
    
        class Myconn implements ServiceConnection{
    
            //当成功绑定的时候调用此对象
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Mybinder  myIbinder =(Mybinder) service;
                myServer = myIbinder.getServer();
            }
            //当解除服务时
            @Override
            public void onServiceDisconnected(ComponentName name) {
                myConn = null;
                myServer = null;
            }
    
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            //意图 接收器回调 标志
            bindService(intent, myConn, Context.BIND_AUTO_CREATE);
        }
        @Override
        protected void onStop() {
            super.onStop();
            //解除绑定服务
            unbindService(myConn);
        }
    
    
    }
    

    方式二生命周期
    1 onCreate()
    2 onBind(Intent intent)
    3 onUnbind(Intent intent)
    4 onDestroy()

  • 启动服务在两个不同程序:
  • AIDL
原文地址:https://www.cnblogs.com/muyuge/p/6152189.html