我試圖從廣播接收器啓動狀態欄通知,然後停止來自另一個廣播接收器,但我遇到了問題。我想開始狀態欄中的通知,當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);
}
}
我有那實際導入,我仍然得到它是未定義的,它想創建方法 – user577732
沒有人有任何想法?真的很感謝一些幫助 – user577732