Alarmmanager通知不起作用

时间:2016-01-29 16:30:08

标签: android broadcastreceiver alarmmanager

我有一个告警管理员在某个日期和时间显示通知。 我安排了这样的通知:

  private void scheduleNotification(String title, String text, String subtext, long futureInMillis) {

    Intent notificationIntent = new Intent(getActivity(), NotificationPublisher.class);
    notificationIntent.putExtra("title", title);
    notificationIntent.putExtra("text", text);
    notificationIntent.putExtra("subtext", text);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
}

广播接收者:

public class NotificationPublisher extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        Log.v("receiver", "notification received");
        String title = intent.getStringExtra("title");
        String text = intent.getStringExtra("text");
        String subtext = intent.getStringExtra("subtext");

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        builder.setSmallIcon(R.drawable.checkbox_on_24dp);
        builder.setContentTitle(title);
        builder.setContentText(text);
        builder.setSubText(subtext);
        builder.setSound(uri);
        builder.setVibrate(new long[]{1000});

        Notification not = builder.build();

        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);


        notificationManager.notify(0, not);

    }
}

清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.christophbielen.simplytodo">
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <provider
        android:name=".data.DataProvider"
        android:authorities="com.christophbielen.simplytodo" />

    <activity
        android:name=".DetailActivity"
        android:label="@string/title_activity_detail"
        android:parentActivityName=".MainActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.christophbielen.simplytodo.MainActivity" />
    </activity>
    <activity
        android:name=".ListCategoriesActivity"
        android:label="@string/title_activity_list_categories"
        android:parentActivityName=".MainActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.christophbielen.simplytodo.MainActivity" />
    </activity>
    <activity
        android:name=".AddListActivity"
        android:label="@string/title_activity_add_list"
        android:parentActivityName=".MainActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.christophbielen.simplytodo.MainActivity" />
    </activity>
    <activity
        android:name=".EditListActivity"
        android:label="@string/title_activity_edit_list"
        android:parentActivityName=".MainActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.christophbielen.simplytodo.MainActivity" />
    </activity>
    <activity
        android:name=".AddItemActivity"
        android:label="@string/title_activity_add_item"

        android:theme="@style/AppTheme.NoActionBar">
        <!--<meta-data-->
            <!--android:name="android.support.PARENT_ACTIVITY"-->
            <!--android:value="com.christophbielen.simplytodo.MainActivity" />-->
    </activity>
    <activity
        android:name=".EditItemActivity"
        android:label="@string/title_activity_edit_item"

        android:theme="@style/AppTheme.NoActionBar">

    </activity>
    <receiver android:name=".NotificationPublisher" />
</application>

我打电话给这样的日程通知:

int year = Integer.parseInt(date.substring(0,4));
                        int month = Integer.parseInt(date.substring(4,6))-1;
                        int day = Integer.parseInt(date.substring(6,8));
                        int hour = Integer.parseInt(time.substring(0,2));
                        int minute = Integer.parseInt(time.substring(2));

                        Calendar cal=Calendar.getInstance();
                        cal.set(Calendar.MONTH,month);
                        cal.set(Calendar.YEAR,year);
                        cal.set(Calendar.DAY_OF_MONTH,day);
                        cal.set(Calendar.HOUR_OF_DAY,hour);
                        cal.set(Calendar.MINUTE,minute);
                        cal.set(Calendar.SECOND,0);

                        String subcontent = catName + " due on: " + year + "/" + month+1 + "/" + day + " " + hour + ":" + minute;

                        scheduleNotification("To do!", shoppingListName, subcontent, cal.getTimeInMillis());

当我安排通知时,它似乎有效但接收器从未收到任何信息。

我到处寻找答案,我无法弄清楚警报器无法正常工作的原因。

由于

2 个答案:

答案 0 :(得分:0)

请使用 -

alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, (System.currentTimeMillis() + futureInMillis, pendingIntent);

而不是 -

alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);

答案 1 :(得分:0)

使用ELAPSED_REALTIME_WAKEUP计划闹钟时,必须使用SystemClock.elapsedRealtime()作为时间的基本偏移量。

AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + futureInMillis, pendingIntent);

DroidFiddle示例:https://droidfiddle.net/vwkttsz/1