日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

android receiver 通知,android – 来自BroadcastReceiver的呼叫通知

發布時間:2023/11/27 生活经验 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android receiver 通知,android – 来自BroadcastReceiver的呼叫通知 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我有代碼:

public void AlarmStart() {

Calendar cal = Calendar.getInstance();

cal.add(Calendar.MINUTE, 5);

Intent intent = new Intent(MainNote.this, AlarmReceiver.class);

intent.putExtra("alarm_message", "MESS");

PendingIntent sender = PendingIntent.getBroadcast(MainNote.this, 1,

intent, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

}

它按時調用AlarmReceiver類.

public class AlarmReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

Bundle bundle = intent.getExtras();

String message = bundle.getString("alarm_message");

NotifierHelper.sendNotification(?????, MainNote.class, "ba", "baba",

2, true, true);

} // Problem here

}

然后NotifierHelper類:

public class NotifierHelper {

private static final int NOTIFY_1 = 0x1001;

public static void sendNotification(Activity caller,

Class> activityToLaunch, String title, String msg,

int numberOfEvents, boolean flashLed, boolean vibrate) {

NotificationManager notifier = (NotificationManager) caller

.getSystemService(Context.NOTIFICATION_SERVICE);

final Notification notify = new Notification(R.drawable.icon, "",

System.currentTimeMillis());

notify.icon = R.drawable.icon;

notify.tickerText = "New Alerts";

notify.when = System.currentTimeMillis();

notify.number = numberOfEvents;

notify.flags |= Notification.FLAG_AUTO_CANCEL;

if (flashLed) {

// add lights

notify.flags |= Notification.FLAG_SHOW_LIGHTS;

notify.ledARGB = Color.CYAN;

notify.ledOnMS = 500;

notify.ledOffMS = 500;

}

if (vibrate) {

notify.vibrate = new long[] { 100, 200, 200, 200, 200, 200, 1000,

200, 200, 200, 1000, 200 };

}

Intent toLaunch = new Intent(caller, activityToLaunch);

PendingIntent intentBack = PendingIntent.getActivity(caller, 0,

toLaunch, 0);

notify.setLatestEventInfo(caller, title, msg, intentBack);

notifier.notify(NOTIFY_1, notify);

}

}

如何從AlarmReceiver傳遞Activity調用者?

解決方法:

我認為你不需要在NotifierHelper中引用任何Activity.使用Context(Activity是其子類),例如:

public static void sendNotification(Context caller, ...

getSystemService()等方法實際上是由Context公開的.

因為你在AlarmReceiver.onReceive()中傳遞了一個Context,你可以傳遞它.

標簽:android,android-activity,notifications,alarm

來源: https://codeday.me/bug/20190518/1128417.html

總結

以上是生活随笔為你收集整理的android receiver 通知,android – 来自BroadcastReceiver的呼叫通知的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。