最近做的一个需求需要应用能在关闭或者进程杀掉的情况下依然可以接收通知并根据通知条件拉起应用。考虑此场景应最大程度为应用保活,由于8.0之后android禁止应用在后台拉起服务,尝试拉起一个前台服务。拉起前台服务的过程还需要设置一个通知,可将通知设置为常驻。
AndroidManifest.xml注册服务
<service
android:name="com.test.AndroidNewBeta.TestService"
android:exported="false"/>
前台服务:会在通知一栏显示 ONGOING 的 Notification, 当服务被终止的时候,通知一栏的 Notification 也会消失,这样对于用户有一定的通知作用。
启用前台服务:
Intent intent = new Intent(this, TestService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
} else {
startService(intent);
}
注意:
-如果在 5 秒内没有调用startForeground(),timeout 就会触发,会报出ANR
一旦通过startForegroundService() 启动前台服务,必须在service 中有startForeground() 配套,不然会出现ANR 或者crash
startForeground() 中的id 和notification 不能为0 和 null
public class TestService extends Service {
private static final String TAG = "TestService";
private static final int NOTIFICATION_ID = 1;
private Timer mTimer;
private NotificationManager mNotificationManager;
private NotificationCompat.Builder mBuilder;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mNotificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
//Android 8.0开始要设置通知渠道
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel("test",
"message",NotificationManager.IMPORTANCE_DEFAULT);
mNotificationManager.createNotificationChannel(channel);
}
//获取 PendingIntent 对象,NotificationActivity是另一个活动
Intent intent = new Intent(this, AndroidActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0 ,intent, 0);
//创建通知
mBuilder = new NotificationCompat.Builder(this,"test")
.setContentTitle("这是通知标题")
.setContentText("这是通知内容")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.icon)
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.icon))
.setContentIntent(pi)
.setAutoCancel(false)
.setOngoing(true);
//发送通知( id唯一,可用于更新通知时对应旧通知; 通过mBuilder.build()拿到notification对象 )
mNotificationManager.notify(NOTIFICATION_ID,mBuilder.build());
startForeground(NOTIFICATION_ID,mBuilder.build());
mTimer = new Timer();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
Log.i(TAG,"1115 onCreate 设置更新Timer");
postRequest();
}
}, 0, 10000);
Log.i(TAG,"1116 onCreate");
}
private void postRequest(){
//网络请求
}
/**
* 追加文件:使用FileWriter
* @param fileName
* @param content
*/
public static void writeLog(String fileName,String content) {
String path = Environment.getExternalStorageDirectory() + "/Log/"; //文件路径
FileWriter writer = null;
try {
File file = new File(path);
if (!file.exists()) { //没有创建文件夹则创建
file.mkdirs();
}
File logFile = new File(path + fileName);
if(!logFile.exists()){
logFile.createNewFile();
}
// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
writer = new FileWriter(path + fileName, true);
writer.write(content + "\r\n");
writer.flush();
if (writer != null) {
//关闭流
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG,"1116 onStartCommand");
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
mNotificationManager.cancel(NOTIFICATION_ID);
mTimer.cancel();
mTimer = null;
Log.i(TAG,"1116 onDestroy");
}
}