2016-05-16 102 views
-1

我的AlertReceiver类正在创建通知服务。我如何取消创建的通知?如何取消BroadcastReceiver的通知

这里是我的代码:

public class AlertReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     createNotification(context,"Title","This is message body", "Alert"); 
    } 
    public void createNotification(Context context,String msg,String msgText, String msgAlert){ 
     PendingIntent notificIntent=PendingIntent.getActivity(context, 0, 
       new Intent(context,MainActivity.class),0); 
     NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(context) 
       .setSmallIcon(R.drawable.my_image) 
       .setContentTitle(msg) 
       .setTicker(msgAlert) 
       .setContentText(msgText); 
     mBuilder.setContentIntent(notificIntent); 
     mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND); 
     mBuilder.setAutoCancel(true); 
     NotificationManager mNotificationManager= 
       (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     mNotificationManager.notify(1,mBuilder.build()); 

    } 
} 
+0

我的意思是我怎么能写取消方法为该类... –

回答

1

使用unregisterReceiver(广播接收器接收器)作为一个单独的功能,从你的类注销广播接收器类。

public void unRegisterAlertreceiver(){ 
    unregisterReceiver(this); 
} 
+0

我怎样才能创建的接收器之间区分... –

+0

我会创造外部类的广播接收机的单独实例。 –

+0

你可以写一个示例代码。这将是非常有用的 –