2011-07-20 36 views
7

我试图从广播接收器启动状态栏通知,然后停止来自另一个广播接收器,但我遇到了问题。我想开始状态栏中的通知,当USB连接,然后当USB断开连接,我想停止它我有两个接收器设置和工作只是努力开始和停止一个接收器这里是代码我目前开始和停止来自广播接收器的通知

我唯一的错误与我的代码是myNotificationManager = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE);行错误只是说getSystemService是未定义的,它想要做的方法,我猜的意思是接收器不支持该方法就像一个活动会所以我应该怎么做才能创造和停止接收通知感谢您的帮助

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); 
     } 
} 

然后是REC eiver为当断开这个我相信是好的,应该工作,我想我的问题是只有在USBCONNECT类

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); 
    } 
} 

回答

9

嗯,我最终想出来自己与我同样的问题,所有我需要做的人将来参考是添加上下文getSystemService的面前,这里是我的代码工作

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); 
     } 
} 

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

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); 
    } 
} 
0

getSystemService不确定很可能意味着你还没有进口android.app.Activity(getSystemService被定义android.app.Activity)。

+0

我有那实际导入,我仍然得到它是未定义的,它想创建方法 – user577732

+0

没有人有任何想法?真的很感谢一些帮助 – user577732

3

我遇到ŧ他自己的问题。我认为用户忘了更新答案中的代码。 context需要在两次!我第一次误读了!

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

希望这可以帮助任何人类似困惑!

0

这是使用通知从broadcastReceivers方式:从BroadcastReceivers

NotificationManager mgr = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 

附表报警:

AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 

你必须始终使用上下文前缀!