每天12小时重复通知 [英] Repeat notification every day 12h

查看:73
本文介绍了每天12小时重复通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想每天12小时重复我的通知,但是我的代码无法正常工作...我在OnCreate的MainActivity中启动警报管理器,如下所示:

I want repeat my notification every day at 12h, but my code isn't working... I launch my Alarm Manager in MainActivity in OnCreate like this :

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类:

And My 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());



}

}

您知道问题出在哪里吗?请不要讨厌我只想学习代码:(

Do you know where is the problem ? Please don't dislike I just want learn code :(

推荐答案

更新清单文件以使接收者具有以下参数:

Update your manifest file to have the following parameters for your receiver:

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

然后,我相信问题可能出在您选择的图标上:

Then I believe the problem could be with the icon you have chosen with:

.setSmallIcon(R.drawable.photo)

如果图像不兼容,则您不会在应用程序外部看到崩溃,如果插入了电话,它将仅在android显示器内部显示致命异常,因此它可能会被忽略并且不会收到通知被解雇.

If the image is not compatible then you will not see a crash outside the app, it will only show a fatal exception inside of the android monitor if the phone is plugged in, so it can go unnoticed and the notification will not be fired.

对于不同的电话密度,所使用的可绘制对象必须具有不同的大小.要制作正确的可绘制对象,请在您的 drawable 包上单击鼠标右键,然后执行New-> Image Asset.从下拉菜单中选择通知图标",然后使用它来生成图标的所有不同大小.

The drawable that is used needs to have the different sizes for different phone densities. To make the correct drawables, right click on your drawable package and do New -> Image Asset. From the pulldown select Notification Icons, and use that to generate all the different sizes for the icon.

这篇关于每天12小时重复通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆