[英]Android Notification AlarmManager and BroadcastReceiver
My AlarmManager does not seem to be working. 我的AlarmManager似乎无法正常工作。
This is my code for setting up the alarm: 这是我设置警报的代码:
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);
This is my Notify
class: 这是我的
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());
}
} }
And I have the following in my Manifest: 我的清单中有以下内容:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<receiver android:name=".Notify" android:enabled="true" />
I don't have any exceptions but the notifications are not showing. 我没有任何例外,但通知没有显示。
Use this code to set the alarm 使用此代码设置警报
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.