繁体   English   中英

每天重复通知 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 类:

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)

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

使用的 drawable 需要针对不同的手机密度具有不同的尺寸。 要制作正确的 drawable,请右键单击您的drawable包并执行 New -> Image Asset。 从下拉列表中选择通知图标,并使用它来生成图标的所有不同大小。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM