每天重复通知 12h

Repeat notification every day 12h

我想每天 12 点重复我的通知,但我的代码不起作用... 我在 OnCreate 的 MainActivity 中启动我的警报管理器,如下所示:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ma);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 12);
    calendar.set(Calendar.MINUTE, 00);
    calendar.set(Calendar.SECOND, 0);
    Intent intent1 = new Intent(MainActivity.this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

还有我的 AlarmReceiver Class :

public class AlarmReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(context, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
            context).setSmallIcon(R.drawable.photo)
            .setContentTitle("ça marche fdp")
            .setContentText("Et ouai t'as bien réussi à afficher une notification bordel de cul").setSound(alarmSound)
            .setAutoCancel(true).setWhen(when)
            .setContentIntent(pendingIntent)
            .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
    notificationManager.notify(0, mNotifyBuilder.build());



}

}

你知道问题出在哪里吗?请不要嫌弃我只想学习代码:(

更新您的清单文件,使您的接收器具有以下参数:

<receiver
        android:name="com.example.alarmmanagernotifcation.AlarmRecei‌​ver"
        android:enabled="true"
        android:process=":remote" />

那么我认为问题可能出在您选择的图标上:

.setSmallIcon(R.drawable.photo)

如果图像不兼容,那么您将不会在应用程序外部看到崩溃,如果插入 phone,它只会在 android 监视器内部显示致命异常,所以它可能会被忽视,并且不会触发通知。

所使用的可绘制对象需要针对不同的 phone 密度具有不同的尺寸。要制作正确的可绘制对象,请右键单击 drawable 包并执行新建 -> 图像资源。从下拉 select 通知图标,并使用它来生成所有不同大小的图标。