我们现在有两个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:enabled="true" android:exported="true"></service> </application> </manifest>
3、在ApkA中设置查询权限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.helloworld"> <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> <!-- <uses-permission android:name="com.permission.MediaMaterial"/>--> <queries> <package android:name="com.example.test001"></package></package> </queries> </manifest>
4、在ApkA中启动Service
Context context = getActivity(); Intent startIntent = new Intent(); Log.i("---test---", "start service begin"); // startIntent.setAction("android.intent.action.START_SERVICE"); startIntent.setComponent(new ComponentName("com.example.test001", "com.example.test001.MyServicetest")); try { // context.startService(startIntent); context.startForegroundService(startIntent); } catch (Exception e) { Log.i("---test---", "error msg:" + e.getMessage()); }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/279848.html