Android 开机自动启动一个Service
一、Google在更新Android8.0后对Service的权限越发收紧。导致目前想要启动服务必需实现服务的前台化,打开应用才能顺利启动service,否则在服务启动5秒后,系统将自动报错,会弹出一个提示窗口。实际测试得系统的app才能启动service。
二、android studio demo源码测试。
1、添加权限,接受开机完成广播,一定的增加android:persistent="true"属性,AndroidManifest.xml源码如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.giadawatchdog">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:persistent="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name=".WatchdogService">
</service>
</application>
</manifest>
2、接受广播处理文件,BootBroadcastReceiver.java
package com.example.giadawatchdog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootBroadcastReceiver extends BroadcastReceiver {
private static final String TAG ="WATCHDOG BROAD";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive !!");
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
Intent bootServiceIntent = new Intent(context, WatchdogService.class);
Log.d(TAG, "onReceive BOOT_COMPLETED!! ");
context.startService(bootServiceIntent);//startForegroundService
//context.startForegroundService(bootServiceIntent);//startForegroundService
}
}
}
3、添加service WatchdogService.java文件。
package com.example.giadawatchdog;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.IBinder;
import android.util.Log;
import java.util.Timer;
import java.util.TimerTask;
public class WatchdogService extends Service {
private static final String TAG ="WATCHDOG SERVICE";
private Timer timer = null;
private int count=0;
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind !!");
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate !!");
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
count++;
Log.d(TAG, "timer 200ms !!count="+count);
}
}, 1000,200);
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.d(TAG, "onStart !!");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy !!");
}
}
4、一定是系统app才可以,adb push到system/app/目录下。
5、运行结果如下:
6、开机自动会运行该app
三、源码路径:https://download.csdn.net/download/qq_37858386/12769083