问题描述
我的目标是使用户能够将某个应用程序轻扫开并关闭前台服务。我通过重写onTaskRemoved来做到这一点。这可以按预期工作,并且我从该方法关闭了我的服务,但是现在存在系统使用onTaskRemoved关闭前台服务的问题。
忘记我使用的设置,但是我的应用在待机应用中被列为免税项目。
我会按照开发人员指南的内容进行操作。
Intent intentServiceMain = new Intent(ActivityMain.this,ServiceMain.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intentServiceMain);
} else {
startService(intentServiceMain);
}
@Override
public int onStartCommand(Intent intent,int flags,int startId) {
Toast.makeText(getApplicationContext(),"Service starting",Toast.LENGTH_SHORT).show();
setUpNotificationBuilder();
notification = notificationCompatBuilder.build();
startForeground(NOTIFICATION_CHANNEL_ID.hashCode(),notification);
return START_STICKY;
}
private void setUpNotificationBuilder() {
notificationCompatBuilder = new NotificationCompat.Builder(
getApplicationContext(),NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.music_note_black_48dp)
.setContentTitle(NOTIFICATION_CHANNEL_ID)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle());
remoteViewsNotificationLayout =
new RemoteViews(getPackageName(),R.layout.pane_notification);
notificationCompatBuilder.setCustomContentView(remoteViewsNotificationLayout);
Intent notificationIntent = new Intent(getApplicationContext(),ActivityMain.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
getApplicationContext(),notificationIntent,0);
notificationCompatBuilder.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = notificationmanager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,NOTIFICATION_CHANNEL_ID,importance);
String description = "Service";
channel.setDescription(description);
notificationmanager notificationmanager = getSystemService(notificationmanager.class);
notificationmanager.createNotificationChannel(channel);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)