android 新增service启动

我们现在有两个APP(ApkA,ApkB),APKB中定义了一个service,APKA启动这个service

1、首先在ApkB中定义service类

package com.example.test001;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class MyServicetest extends Service {
    private static final String TAG = MyServicetest.class.getSimpleName() + "---test---";
    IBinder mBinder = new MediaMaterialBinder1();

    public MyServicetest() {}

    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "---test---onBind");
        return mBinder;
    }

    /**
     * media material Binder
     */
    public class MediaMaterialBinder1 extends Binder {
        MyServicetest getService() {
            return MyServicetest.this;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "---test---onCreate001");
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.i(TAG, "---test---onStart");

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.v(TAG, "---test---onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        super.onUnbind(intent);
        Log.i(TAG, "---test---onUnbind");
        return false;
    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
        Log.i(TAG, "---test---onRebind");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "---test---onDestroy");
    }
}

 

2、在在ApkB中Manifest中定义service

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.test001">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.test001"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".MyServicetest"
            android:permission="com.example.test001.permission"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.test001.action.START_APP" />
                <category android:name="com.example.test001.category.START_APP" />
            </intent-filter>
        </service>
    </application>
</manifest>

 

 

 

3、在ApkA中设置查询权限

其中queries中定义的访问包权限一定要给,如果service定义了permission,那ApkA中也要use该permission。

也可以获取所有包权限,<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.helloworld">
    <uses-permission android:name="com.example.test001.permission"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.HelloWorld">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.HelloWorld.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
<queries>
    <package android:name="com.example.test001"></package>
</queries>
</manifest>

 

 

4、在ApkA中启动Service

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, " onServiceDisconnected");
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, " onServiceConnected." + service);
        }
    };

    public void bindServiceF() {
        Intent intent = new Intent("com.example.test001.action.START_APP").addCategory("com.example.test001.category.START_APP")
                .setPackage("com.example.test001");
        boolean result = context.bindService(intent, mConnection,
                Context.BIND_AUTO_CREATE | Context.BIND_ADJUST_WITH_ACTIVITY);
        Log.d(TAG, " bind result:" + result);
    }
1)Service中需要创建一个实现IBinder的内部类(这个内部类不一定在Service中实现,但必须在Service中创建它)。在OnBind()方法中需返回一个IBinder实例,不然onServiceConnected方法不会调用。
2)ServiceConnection 的回调方法onServiceDisconnected() 在连接正常关闭的情况下是不会被调用的, 该方法只在Service 被破坏了或者被杀死的时候调用. 例如, 系统资源不足, 要关闭一些Services, 刚好连接绑定的 Service 是被关闭者之一,  这个时候onServiceDisconnected() 就会被调用。
3)bindService(): 通过该方法绑定服务,需要传入三个参数:
    Intent:Intent 对象;
    ServiceConnection 对象;
    int:绑定服务的类型:
        Context.BIND_AUTO_CREATE:绑定的 Service 不存在时,会自动创建;
        Context.BIND_ADJUST_WITH_ACTIVITY:Service 的优先级别与所绑定的Activity 的重要程度有关,Activity 处于前台时,Service 的级别高;
        Context.BIND_NOT_FOREGROUND:Service 永远不会拥有运行前台的优先级别;
        Context.BIND_WAIVE_PRIORITY:Service 的优先级别不会改变;
        Context.BIND_IMPORTANT | BIND_ABOVE_CLIENT:Service 所绑定的 Activity 处于前台时,Service 也处于前台;BIND_ABOVE_CLIENT 指定内存很低的情况下,运行时在终止绑定的 Service 之前终止 Activity;
每次unbindService之后再重新bindService,onServiceConnected中得到的service实例会变化,但是service所在的进程号一直不变

 

5、最后现在自定义的APK不允许关联启动,自己调试的时候可以在  手机管家----应用启动管理----APKB,关闭自动管理,把允许自启动、允许关联启动、允许后台启动打开,然后在ApkA中调用启动服务接口,就可以后台打开ApkB中的服务了

报错内容:

Service starting has been prevented by iaware or trustsbase sInfo ServiceInfo{6af6a6d com.example.test001.service.MyServicetest}

posted @ 2022-08-11 10:23  SaraMorning  阅读(700)  评论(0编辑  收藏  举报