gpt4 book ai didi

android - 如何在节电程序在android上杀死它后重新启动粘性服务

转载 作者:行者123 更新时间:2023-11-29 19:15:27 24 4
gpt4 key购买 nike

我尝试通过设置 android:isolatedProcess="true" 但它不起作用

其实我想一直显示永久通知

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)

.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), resourceIcon/*R.mipmap.ic_launcher*/))
.setSmallIcon(R.drawable.ic_icon_flo_not)
.setContentTitle(title)
.setOngoing(onGoing)
.setAutoCancel(autoCancelable)
.setPriority(priority)
.setContentText(message);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (playSound)
mBuilder.setSound(soundUri);
if (remoteViews == null) {
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, resultClass);
resultIntent.setAction(action);

// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
} else {
mBuilder.setContent(remoteViews);
}

最佳答案

在您的服务中覆盖 onTaskRemoved() 并使用警报管理器再次启动服务。下面是代码。

    @Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);

Log.d(TAG, "TASK REMOVED");

PendingIntent service = PendingIntent.getService(
getApplicationContext(),
1001,
new Intent(getApplicationContext(), MyService.class),
PendingIntent.FLAG_ONE_SHOT);

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, service);
}

关于android - 如何在节电程序在android上杀死它后重新启动粘性服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43672853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com