如何在最近的应用中滑动后关闭应用程序(或仅删除所有通知)?在API级别< = 22,它自动工作(只是强制退出应用程序)但API级别23 - 刷卡后的棉花糖+应用程序未关闭且通知也未关闭。
如何在recnet应用程序中滑动后杀死通知或应用程序?
我正在显示onPause通知并在onResume上删除它...但是当我在最近的应用程序中滑动应用程序时(应用程序已关闭 - 重新打开应用程序后有新的初始化)但通知仍然在这里,我需要杀死这个通知。
感谢您的回答。
BTW:我尝试了服务(onTaskRemoved())但是这个函数永远不会被调用,我也不知道为什么......
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private static final String TAG = "VYKAL";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand()");
return START_NOT_STICKY;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Log.i(TAG, "onTaskRemoved()");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy()");
}
@Override
public void onLowMemory() {
super.onLowMemory();
Log.i(TAG, "onLowMemory()");
}
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent serviceIntent = new Intent(MainActivity.this, MyService.class);
startService(serviceIntent);
编辑:通知
Intent notificationIntent = new Intent(compat, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(compat, 0,
notificationIntent, 0);
mBuilder = new NotificationCompat.Builder(compat)
.setSmallIcon(getSmallIcon(laststate))
.setContentText(lastcontent)
.setContentTitle(compat.getString(R.string.APPLICATION_NAME))
.setContentIntent(intent).setOngoing(true);
RemoteViews rv = new RemoteViews(compat.getPackageName(), R.layout.notification_layout);
rv.setImageViewResource(R.id.imagenotileft, getLargeIconSRC(laststate));
rv.setTextViewText(R.id.title, compat.getString(R.string.APPLICATION_NAME));
rv.setTextViewText(R.id.text, lastcontent);
mBuilder.setContent(rv);
NotificationManager manager = (NotificationManager) compat.getSystemService(NOTIFICATION_SERVICE);
manager.notify(0, mBuilder.build());
答案 0 :(得分:2)
解决方案是一个带有onTaskRemoved()的新服务...示例:
echo clean('a|"bc!@£de^&$f g');