【问题标题】:Creating a Notification at a particular time through Alarm Manager通过警报管理器在特定时间创建通知
【发布时间】:2013-11-13 21:07:43
【问题描述】:

我正在尝试在特定时间创建通知。我创建了一个广播接收器并通过 AlarmManager 调用它。问题是没有收到广播并且我没有收到任何通知。

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

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

}

Main Activity 中创建警报的代码。

/**
     * 
     * @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);
    }

谁能指出并纠正我做错了什么?谢谢。

【问题讨论】:

  • 我的回答对您没有帮助吗?

标签: android broadcastreceiver alarmmanager android-pendingintent android-alarms


【解决方案1】:

您在 Manifest 中对 Receiver 的定义有误。

试试这个:

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

android:name 中,您必须指定接收器的绝对包+类,或者您在清单的根元素中指定的package="com.example.android" 的相对值。例如:

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

接收者的动作部分用于指定要监听的动作。所以你也可以这样做:

基于Action而不是类创建一个Intent

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

然后您可以在接收器中处理多个操作:

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

但是,您必须以这种方式在清单中声明您的接收者:

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

你可以同时使用这两种方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多