开始,并从广播接收器停止的通知 [英] Starting and stopping a notification from broadcast receiver

查看:160
本文介绍了开始,并从广播接收器停止的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开始从广播接收器的状态栏通知,然后从另外一个广播接收器阻止它,但我有问题。我想先通知中的状态栏时,USB连接,然后当USB断开时,我想阻止它我有两个接收器设置和工作得挣扎开始,并从这里接收器停止一个是code口有目前

我和我的code唯一的错误就是 myNotificationManager =(NotificationManager)getSystemService(context.NOTIFICATION_SERVICE); 行中的出错只是说getSystemService是不确定的,它希望使我的猜测是指接收方不具有像一个活动的方法支持该方法会如此我应该怎么做才能创造和停止的通知,从接收机感谢您的帮助

 公共类USBCONNECT扩展的BroadcastReceiver {

公共NotificationManager myNotificationManager;
公共静态最终诠释NOTIFICATION_ID = 1;

@覆盖
公共无效的onReceive(上下文的背景下,意图意图){

    myNotificationManager =(NotificationManager)getSystemService(context.NOTIFICATION_SERVICE);

      CharSequence的NotificationTicket =USB连接!;
      CharSequence的NotificationTitle =USB连接!;
      CharSequence的NotificationContent =USB连接!;

      通知通知=新的通知(R.drawable.usbicon,NotificationTicket,0);
      意图notificationIntent =新的意图(背景下,MyClass.class);
      PendingIntent contentIntent = PendingIntent.getActivity(上下文,0,notificationIntent,0);
      notification.setLatestEventInfo(背景下,NotificationTitle,NotificationContent,contentIntent);
      notification.flags | = Notification.FLAG_ONGOING_EVENT;
      myNotificationManager.notify(NOTIFICATION_ID,通知);
      }
}
 

,然后接收器时,它切断这一点,我相信,是好的,应该工作,我想我的问题是只有在USBCONNECT类

 公共类USBDisCon扩展的BroadcastReceiver {

@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
    NotificationManager notificationManager =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(USBConnect.NOTIFICATION_ID);
    }
}
 

解决方案

嗯,我最终计算出来自己的人跟我同样的问题,所有我需要做备查在getSystemService前面加上背景在这里是我的工作code

 公共类USBCONNECT扩展的BroadcastReceiver {

公共NotificationManager myNotificationManager;
公共静态最终诠释NOTIFICATION_ID = 1;

@覆盖
公共无效的onReceive(上下文的背景下,意图意图){

    myNotificationManager =(NotificationManager)getSystemService(context.NOTIFICATION_SERVICE);

      CharSequence的NotificationTicket =USB连接!;
      CharSequence的NotificationTitle =USB连接!;
      CharSequence的NotificationContent =USB连接!;

      通知通知=新的通知(R.drawable.usbicon,NotificationTicket,0);
      意图notificationIntent =新的意图(背景下,MyClass.class);
      PendingIntent contentIntent = PendingIntent.getActivity(上下文,0,notificationIntent,0);
      notification.setLatestEventInfo(背景下,NotificationTitle,NotificationContent,contentIntent);
      notification.flags | = Notification.FLAG_ONGOING_EVENT;
      myNotificationManager.notify(NOTIFICATION_ID,通知);
      }
}
 

,然后接收器时,它切断这一点,我相信,是好的,应该工作,我想我的问题是只有在USBCONNECT类

 公共类USBDisCon扩展的BroadcastReceiver {

@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
    NotificationManager notificationManager =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(USBConnect.NOTIFICATION_ID);
    }
}
 

I'm trying to start a status bar notification from a broadcast receiver and then stop it from another broadcast receiver but I'm having issues. I would like to start the notification in the status bar when usb is connected and then when usb is disconnected I would like to stop it I have the two receivers set up and working just struggling with starting and stopping one from a receiver here is the code I have currently

My only error with my code is the myNotificationManager = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE); line the error just says getSystemService is undefined and it wants to make the method which I guess means that the receiver doesn't have support for that method like an activity would so what should I do to create and stop a notification from receivers thank you for any help

public class USBConnect extends BroadcastReceiver {

public NotificationManager myNotificationManager;
public static final int NOTIFICATION_ID = 1;

@Override
public void onReceive(Context context, Intent intent) {

    myNotificationManager = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE);

      CharSequence NotificationTicket = "USB Connected!";
      CharSequence NotificationTitle = "USB Connected!";
      CharSequence NotificationContent = "USB is Connected!";

      Notification notification = new Notification(R.drawable.usbicon, NotificationTicket, 0);
      Intent notificationIntent = new Intent(context, MyClass.class);
      PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
      notification.setLatestEventInfo(context, NotificationTitle, NotificationContent, contentIntent);
      notification.flags |= Notification.FLAG_ONGOING_EVENT;
      myNotificationManager.notify(NOTIFICATION_ID, notification);
      }
}

And then the receiver for when it disconnects this I believe is fine and should work I think my problem is only in the USBConnect class

public class USBDisCon extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.cancel(USBConnect.NOTIFICATION_ID);
    }
}

解决方案

Well i ended up figuring it out myself for future reference for people with my same issue all i needed to do was add context in front of getSystemService here is my code that worked

public class USBConnect extends BroadcastReceiver {

public NotificationManager myNotificationManager;
public static final int NOTIFICATION_ID = 1;

@Override
public void onReceive(Context context, Intent intent) {

    myNotificationManager = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE);

      CharSequence NotificationTicket = "USB Connected!";
      CharSequence NotificationTitle = "USB Connected!";
      CharSequence NotificationContent = "USB is Connected!";

      Notification notification = new Notification(R.drawable.usbicon, NotificationTicket, 0);
      Intent notificationIntent = new Intent(context, MyClass.class);
      PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
      notification.setLatestEventInfo(context, NotificationTitle, NotificationContent, contentIntent);
      notification.flags |= Notification.FLAG_ONGOING_EVENT;
      myNotificationManager.notify(NOTIFICATION_ID, notification);
      }
}

And then the receiver for when it disconnects this i believe is fine and should work i think my problem is only in the USBConnect class

public class USBDisCon extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.cancel(USBConnect.NOTIFICATION_ID);
    }
}

这篇关于开始,并从广播接收器停止的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆