我想在每天的特定时间向我的用户显示通知。我已经查看了大量的示例和问题,但我仍然有一个问题 - 通知不会出现。
这是 BroadcastReceiver 类 -
public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {
private Context con;
final public static String ONE_TIME = "onetime";
@Override
public void onReceive(Context context, Intent intent) {
con = context;
showNotification();
}
public void SetAlarm(Context context)
{
Calendar now = Calendar.getInstance();
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(ONE_TIME, Boolean.FALSE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
//After after 30 seconds
am.setRepeating(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), 1000 * 5 , pi);
}
public void CancelAlarm(Context context)
{
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
/**
* Creates a notification and shows it in the OS drag-down status bar
*/
private void showNotification() {
NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(con)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Hello")
.setContentText("Hello")
.setDefaults(Notification.DEFAULT_ALL)
.setTicker("HELLO!")
.setAutoCancel(true);
Intent resultIntent=new Intent(con, ViewMenu.class);
PendingIntent pIntent=PendingIntent.getActivity(con,0,resultIntent,0);
mBuilder.setContentIntent(pIntent);
NotificationManager mNotificationManager =
(NotificationManager) con.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(90, mBuilder.build());
Toast.makeText(con, "TEST4", Toast.LENGTH_LONG).show();
}
}
这就是我在主 Activity 上设置它的方式 -
alarm = new testy.app.Notifications.AlarmManagerBroadcastReceiver();
alarm.SetAlarm(this);
我还在清单中注册了 BroadcastReceiver -
<!-- Broadcast receiver -->
<receiver android:name="testy$app$Notifications$AlarmManagerBroadcastReceiver</receiver>
将不胜感激任何建议,谢谢