Creating a Notification at a particular time through Alarm Manager

情到浓时终转凉″ 提交于 2019-11-30 09:55:10
nsL

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

Try this:

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

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>

You can use both ways.

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