Android 在我的应用程序被终止后,通知将保持不变
我有一个异步任务中的静态媒体播放器 调用Android 在我的应用程序被终止后,通知将保持不变,android,notifications,android-notifications,Android,Notifications,Android Notifications,我有一个异步任务中的静态媒体播放器 调用onPause()时,我会创建一个通知 if(Reproduce.mPlayer!=null && Reproduce.mPlayer.isPlaying() ){ nPanel = new Controler(getApplicationContext()); } 调用onResume()时,通知将消失 if(nPanel != null) { nPanel.notificationCancel(); } 到
onPause()
时,我会创建一个通知
if(Reproduce.mPlayer!=null && Reproduce.mPlayer.isPlaying() ){
nPanel = new Controler(getApplicationContext());
}
调用onResume()
时,通知将消失
if(nPanel != null) {
nPanel.notificationCancel();
}
到目前为止还不错
但是如果我通过滑动通知来终止我的应用程序,我不想这样,我也没有办法避免这样
Pd:如果我从app>force kill中杀死app,通知就会消失
这是我的通知代码
和AndroidManifest.xml
onPause()
将不起作用,因为每次调用时它都会终止通知,并且从不调用onDestroyd()
要通过最近的刷卡来检测应用程序终止,请参见:这肯定会对您有所帮助。基本上,您需要编写自己的服务并重写onTaskRemoved()方法来实现这一点。
public class Controler {
private Context parent;
private NotificationManager nManager;
private NotificationCompat.Builder nBuilder;
private RemoteViews remoteView;
public Controler(Context parent) {
// TODO Auto-generated constructor stub
this.parent = parent;
nBuilder = new NotificationCompat.Builder(parent)
.setContentTitle("Parking Meter")
.setSmallIcon(R.drawable.stock)
.setOngoing(true);
remoteView = new RemoteViews(parent.getPackageName(), R.layout.notificationview);
//set the button listeners
setListeners(remoteView);
nBuilder.setContent(remoteView);
nManager = (NotificationManager) parent.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(2, nBuilder.build());
}
public void setListeners(RemoteViews view){
//listener 1
Intent volume = new Intent(parent,NotificationReturnSlot.class);
volume.putExtra("DO", "volume");
PendingIntent btn1 = PendingIntent.getActivity(parent, 0, volume, 0);
view.setOnClickPendingIntent(R.id.btn1, btn1);
//listener 2
Intent stop = new Intent(parent, NotificationReturnSlot.class);
stop.putExtra("DO", "stop");
PendingIntent btn2 = PendingIntent.getActivity(parent, 1, stop, 0);
view.setOnClickPendingIntent(R.id.btn2, btn2);
}
public void notificationCancel() {
nManager.cancel(2);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
String action = (String) getIntent().getExtras().get("DO");
NotificationManager nMgr = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancel(2);
try{if (action.equals("volume")) {
if(Reproduce.mPlayer != null) {
if (Reproduce.mPlayer.isPlaying()) {
Reproduce.mPlayer.stop();
finish();
}
}
//Your code
} else if (action.equals("stop")) {
//Your code
Log.i("help", "stopNotification");
try {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // You need this if starting
// the activity from a service
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
}catch (Exception e ){}
}
finish();
}catch (Exception e){}
}
<activity android:name=".NotificationReturnSlot"
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true"/>