Android8.0之后Service变为后台后很开就会被杀死。因此要采取一定的措施进行保活。
启动service:
Intent i=new Intent(context,TestIntentService.class);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
context.startForegroundService(i);
}else{
context.startService(i);
}
Service类为:
package com.example.myapplicationww;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
public class TestIntentService extends Service {
private static final String TAG = TestIntentService.class.getSimpleName();
private USBBroadcastReceiver usbBroadcastReceiver;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
//设置点击跳转
Context context=getApplicationContext();
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
String id = "1";
String name = "channel_name_1";
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_DEFAULT);
mChannel.setSound(null, null);
notificationManager.createNotificationChannel(mChannel);
notification = new Notification.Builder(context)
.setChannelId(id)
.setContentTitle(context.getResources().getString(R.string.app_name))
.setContentIntent(pendingIntent)
.setAutoCancel(false)// 设置这个标志当用户单击面板就可以让通知将自动取消
.setOngoing(true)// true,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
.setSmallIcon(R.drawable.ic_launcher_background).build();
} else {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setContentTitle(context.getResources().getString(R.string.app_name))
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_DEFAULT)// 设置该通知优先级
.setAutoCancel(false)// 设置这个标志当用户单击面板就可以让通知将自动取消
.setOngoing(true)// true,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
.setSmallIcon(R.drawable.ic_launcher_background);
notification = notificationBuilder.build();
}
startForeground(1, notification);
Log.d(TAG,"service onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG,"service onStartCommand");
usbBroadcastReceiver=new USBBroadcastReceiver();
/*IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
intentFilter.addAction(BatteryManager.ACTION_CHARGING);
intentFilter.addAction(BatteryManager.ACTION_DISCHARGING);
getApplicationContext().registerReceiver(usbBroadcastReceiver, intentFilter);*/
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG,"service onDestroy");
unregisterReceiver(usbBroadcastReceiver);
}
}
来源:CSDN
作者:The_king_of_Asia
链接:https://blog.csdn.net/The_king_of_Asia/article/details/104036915