问题解决了
添加一个java类文件。这里的文件名是KillNotificationService.java
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
public class KillNotificationService extends Service{
@Override
public void onTaskRemoved(Intent rootIntent) {
//Toast.makeText(this, “service called: “, Toast.LENGTH_LONG).show();
super.onTaskRemoved(rootIntent);
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) getSystemService(ns);
nMgr.cancelAll();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
在您的MainActivity oncreate
方法中调用此服务。
startService(new Intent(this, KillNotificationService.class));
在Manifest文件中添加服务
<service android:name=".KillNotificationService"/>