当我打开应用程序时,我按下主页按钮它仍然在后台播放(它显示一个播放和停止的通知栏),一段时间后我想要它关闭它所以我按下主页按钮,直到它显示我 活动应用程序,然后滑动它关闭,所以如果我这样关闭它我想要我的应用程序的通知消失
如果在按住主屏幕时(当您在设备的主屏幕中时)通过"活动应用程序列表"中的滑动操作关闭,我如何删除与应用程序相关的通知栏?
**当我滑动以关闭应用程序时,未实现我的类中的onDestroy()方法
**第二次意识到我必须调用服务才能到达onTaskRemoved(),在这个方法中我可以实现notificationManager关闭
好的,所以我已经弄清楚了。
首先,我将制作一个服务类
public class MyService extends Service {
@Override
public void onCreate()
{
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
现在我要在onPause()中启动服务
startService(new Intent(this, MyService.class));
然后是onDestroy()方法(在Service的帮助下调用)
@Override
protected void onDestroy()
{
super.onDestroy();
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) this.getSystemService(ns);
nMgr.cancel(1);
}
并且不要忘记添加Manifest
android:stopWithTask="false" >