[英]Creating a notification with BroadcastReceiver and AlarmManager
[英]Android Notification AlarmManager and BroadcastReceiver
我的AlarmManager似乎無法正常工作。
這是我設置警報的代碼:
Intent intent1 = new Intent(this, Notify.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.MINUTE, 20);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
這是我的Notify
類:
public class Notify extends BroadcastReceiver {
public static final int uniqueID=3657554;
@Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder n=new NotificationCompat.Builder(context);
n.setAutoCancel(true);
n.setSmallIcon(R.mipmap.ic_launcher);
n.setTicker("ticker");
n.setWhen(System.currentTimeMillis());
n.setContentTitle("title");
n.setContentText("text");
Intent it=new Intent(context,ListAll.class);
PendingIntent pi=PendingIntent.getBroadcast(context,0, it,0);
n.setContentIntent(pi);
((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)).notify(uniqueID,n.build());
}
}
我的清單中有以下內容:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<receiver android:name=".Notify" android:enabled="true" />
我沒有任何例外,但通知沒有顯示。
使用此代碼設置警報
Intent alarmIntent = new Intent(this, YourActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 20); // '8pm'
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 1);
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.