【问题标题】:Repeat notification every day 12h每天 12 小时重复通知
【发布时间】:2017-01-13 21:30:32
【问题描述】:

我想在每天 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());



}

}

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

【问题讨论】:

标签: android alarmmanager android-notifications


【解决方案1】:

更新您的清单文件,为您的接收器添加以下参数:

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

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

.setSmallIcon(R.drawable.photo)

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

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

【讨论】:

  • 非常感谢,我会努力的 :)
  • 非常感谢它的工作IIIINNGGGG:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-11
相关资源
最近更新 更多