How to show local notification every hour using service

旧时模样 提交于 2019-12-02 14:54:32

问题


I want to show local notification on every hour or particular seconds using service, I have tried to implement this functionality, but I didn't got success.

My code is look like below

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);

回答1:


Your code seems to be OK. The only thing that can be problematic is the interval 4*60*60 is too short which is 14.4 seconds.

Moreover it seems like you're not directing the intent to a specific receiver. you should do like this:

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);

And you should catch it in:

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

}

}

Add this to your manifest file:

 <!-- 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"/> 


来源:https://stackoverflow.com/questions/44861271/how-to-show-local-notification-every-hour-using-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!