Android Service远程启动
远程start:
AIDL1代码
manifest:(重点在permission后台权限和exported)
< ? xml version= "1.0" encoding= "utf-8" ? >
< manifest xmlns: android= "http://schemas.android.com/apk/res/android"
xmlns: tools= "http://schemas.android.com/tools" >
< uses - permission android: name= "android.permission.INTERNET" / >
< uses - permission android: name= "android.permission.FOREGROUND_SERVICE" / >
< 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.AIDL1"
tools: targetApi= "31" >
< service
android: name= ".MyService"
android: enabled= "true"
android: exported= "true" > < / service>
< 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>
< / application>
< / manifest>
MyService
public class MyService extends Service {
public MyService ( ) {
}
@Override
public IBinder onBind ( Intent intent) {
throw new UnsupportedOperationException ( "Not yet implemented" ) ;
}
@Override
public void onCreate ( ) {
super . onCreate ( ) ;
Log . i ( "yishiqi" , "oncreate(i); " ) ;
}
@Override
public void onDestroy ( ) {
super . onDestroy ( ) ;
Log . i ( "yishiqi" , "ondestroy(i); " ) ;
}
}
AIDL2代码
manifest(重点在queries,指定可以访问的包路径)
< ? xml version= "1.0" encoding= "utf-8" ? >
< manifest xmlns: android= "http://schemas.android.com/apk/res/android"
xmlns: tools= "http://schemas.android.com/tools" >
< queries>
< package android : name= "com.example.aidl1" / >
< / queries>
< 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.AIDL2"
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>
< / application>
< / manifest>
activity(重点在 直接使用包名+类全路径名启动)
package com. example. aidl2 ;
import android. content. ComponentName ;
import android. content. Intent ;
import android. os. Bundle ;
import android. util. Log ;
import android. view