通过滑动操作关闭应用程序时删除通知栏

时间:2014-06-25 08:13:48

标签: android service notifications swipe

当我打开应用程序时,我按下主页按钮,它仍然在后台播放(它显示一个播放和停止的通知栏),过了一段时间我希望它关闭它所以我按下主页按钮直到它显示我的活动应用程序然后滑动它关闭,所以如果我这样关闭它我想要我的应用程序的通知消失

如果在按住主屏幕时(当您在主屏幕中时,通过“活动应用程序”列表中的滑动操作关闭,则如何删除与应用程序相关的通知栏)设备)?

**当我滑动关闭应用程序时,实现了我的类中的onDestroy()方法不被调用

**第二次意识到我必须调用服务才能到达onTaskRemoved(),在这个方法中我可以实现notificationManager关闭

1 个答案:

答案 0 :(得分:1)

好的,我已经弄清楚了。


首先,我将制作一个服务类

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

 <service android:name="this.MyPackage.MyService" 
             android:stopWithTask="false"   >            
    </service>
 </application>
相关问题