2011-07-20 92 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); 

你必須始終使用上下文前綴!