【问题标题】:How to show local notification every hour using service如何使用服务每小时显示本地通知
【发布时间】:2017-07-01 13:05:27
【问题描述】:

我想使用服务在每小时或特定秒内显示本地通知,我已尝试实现此功能,但没有成功。

我的代码如下所示

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
        notificationIntent.addCategory("android.intent.category.DEFAULT");

        PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Calendar cal = Calendar.getInstance();
        Log.d("day for notification:::", String.valueOf(notification_day));

            cal.add(Calendar.MINUTE, notification_day);
alarmManager.setRepeating(AlarmManager.RTC, cal.getTimeInMillis(), 4 * 60 * 60, broadcast);

【问题讨论】:

  • 您是否使用过警报管理器和作业调度程序。发布您尝试过的代码。
  • 是的!我用过。
  • 已更新代码!

标签: android service alarmmanager android-notifications show


【解决方案1】:

您的代码似乎没问题。 唯一可能有问题的是 4*60*60 的间隔太短,即 14.4 秒。

此外,您似乎没有将意图指向特定的接收者。你应该这样做:

Calendar calendar = Calendar.getInstance();
Intent intent1 = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager)this.getSystemService(this.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60, pendingIntent);

你应该抓住它:

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, EVentsPerform.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

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


    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
            context).setSmallIcon(R.drawable.applogo)
            .setContentTitle("text")
            .setContentText("text")
            .setWhen(when)
            .setContentIntent(pendingIntent)
    notificationManager.notify(yourId, mNotifyBuilder.build());

}

}

将此添加到您的清单文件中:

 <!-- permission required to use Alarm Manager -->
 <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>

 <!-- Register the Alarm Receiver -->
 <receiver android:name="com.example.alarmmanagernotifcation.AlarmReceiver"/> 

【讨论】:

  • 但是我想在应用关闭时显示通知,所以必须在Service中实现,你知道吗?
  • 接收者会为你做这件事。如果服务目的只是显示您不需要的通知。接收者可以为您代劳。
  • 你在manifest中注册了receiver并在代码中使用了吗?
  • 是的,我已经注册了!
  • 这很奇怪。看看这个教程:karanbalkar.com/2013/07/…
猜你喜欢
  • 2022-01-21
  • 1970-01-01
  • 2017-03-06
  • 1970-01-01
  • 1970-01-01
  • 2019-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多