创建通过报警管理器通知在特定的时间 [英] Creating a Notification at a particular time through Alarm Manager

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

问题描述

我想在特定的时间来创建一个通知。即时创建广播接收机,并通过AlarmManager调用它。问题是,广播没有收到,我没有得到任何通知。

I am trying to create a notification at a particular time. Im creating a broadcast receiver and calling it through the AlarmManager. Problem is that the broadcast is not received and that I am not getting any notifications.

注册广播接收器在清单中,

Registering the Broadcast Receiver in the Manifest,

<receiver
        android:name="com.example.android.receivers">
        <intent-filter>
            <action android:name="com.example.android.receivers.AlarmReceiver" />
        </intent-filter>
    </receiver>

这是广播接收器,

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context pContext, Intent pIntent) {

//      if (pIntent.getAction().equalsIgnoreCase("")) {
//          
//      }
        Log.d("Alarm Receiver", "onReceive called");        
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(pContext).setSmallIcon(R.drawable.icon).setContentTitle("Test Notification")
                        .setContentText("Test Notification made by Syed Ahmed Hussain");
        Intent resultIntent = new Intent(pContext, CalendarView.class);

        // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(pContext);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(CalendarView.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        notificationBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) pContext.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, notificationBuilder.build());
    }

}

code的主要活动,它生成一个警报。

Code from Main Activity which creates an Alarm.

/**
     * 
     * @param pDate
     * @param pTime
     */
    private void createNotification(String pDate, String pTime) {
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, AlarmReceiver.class);
        PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
//      alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 60 * 1000, alarmIntent);
        alarmManager.set(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), alarmIntent);
    }

任何人都可以请指出并纠正我究竟做错了什么?谢谢你。

Can anyone please point out and rectify what am I doing wrong? Thank you.

推荐答案

您的清单接收器的错误定义。

You have a wrong definition of the Receiver in your Manifest.

试试这个:

<receiver android:name="com.example.android.receivers.AlarmReceiver" />

安卓名称您必须指定绝对包+级接收器,或从相对包=为com.example .android您在清单的根元素指定。例如:

in android:name you have to specify the absolute package+class of the receiver, or the relative from the package="com.example.android" you specified in the root element of the Manifest. e.g:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android" >


行动接收器部分是指定要听哪些行动。所以,你也可以这样来做:


Action sections of receivers are to specify which actions to listen. So you can also do it this way:

创建一个基于意图的行动,而不是类

Create an Intent based on the Action instead of the class

public static final String ACTION = "com.example.android.receivers.NOTIFICATION_ALARM";

private void createNotification(String pDate, String pTime) {
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(ACTION);
    PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    alarmManager.set(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), alarmIntent);
}

在接收器

然后就可以处理多个动作:

Then you can handle multiple actions in your receiver:

@Override
public void onReceive(Context pContext, Intent pIntent){
        if (pIntent.getAction().equals(ACTION)){
            // Here your handle code
        }
}

但是,你必须声明你的接收器在清单中是这样的:

But then, you will have to declare your receiver in the manifest this way:

<receiver android:name="com.example.android.receivers">
    <intent-filter>
        <action android:name="com.example.android.receivers.NOTIFICATION_ALARM" />
    </intent-filter>
</receiver>

您可以使用两种方式。

这篇关于创建通过报警管理器通知在特定的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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