在特定的时间AlarmManager显示通知 [英] Show Notification at particular time with AlarmManager

查看:350
本文介绍了在特定的时间AlarmManager显示通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要每日显示通知在特定时间(例如:下午4点25)。我使用 AlarmManager 通知。我设置应用程序的登陆页面上的报警。我也已经实现了广播接收器吧。

I need to display a notification daily at particular time (for example: 4.25PM). I am using AlarmManager and Notification. I set the alarm on landing page of the application. I also have implemented the BroadcastReceiver for it.

code设置报警:

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 16);
calendar.set(Calendar.MINUTE, 25);
calendar.set(Calendar.SECOND, 0);

Intent notificationmassage = new Intent(getApplicationContext(),Notificationmassage.class);

//This is alarm manager
PendingIntent pi = PendingIntent.getService(this, 0 , notificationmassage, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, pi);

这是广播接收机

public class Notificationmassage extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent arg1) {
        showNotification(context);
    }

    private void showNotification(Context context) {
        Log.i("notification", "visible");

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                new Intent(context, Notificationmassage.class), 0);

        NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("xyz")
                .setContentText("It will contain dummy content");
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());
    }  
}

什么是错的这种做法?

What is wrong with this approach?

推荐答案

添加到reciever的manifest.xml如果没有之前做到这一点。

Add reciever to Manifest.xml if you don't do this before.

 <receiver android:name="org.yourapp.Notificationmassage"></receiver>

在应用程序部分。

    <application>
       ....

       <receiver android:name="org.yourapp.Notificationmassage"></receiver>

    </application>

如果你已经做到这一点,请编辑您的问题,并显示您的Manifest.xml

If you already do that, please edit your question and show your Manifest.xml

这篇关于在特定的时间AlarmManager显示通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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