无法使用广播的官方方法



我正在使用警报管理器来调用接收器方法的通知。我在其中放了一些调试代码,发现该过程甚至没有进入那里。

这是我的代码:

sustest.xml

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".Menu">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".ContentInput"
        />
    <receiver
        android:process=":remote"
        android:name=".NoticationBroadcast"/>
</application>

菜单java(主要类(

Button noti = (Button) findViewById(R.id.noti);
noti.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.e(ErrorTag, "before onclick");
        scheduleNotification(getNotification("ggggg"),2000);
        Log.e(ErrorTag, "after onclick");
    }
});
private Notification getNotification(String content) {
    Notification.Builder builder = new Notification.Builder(this);
    builder.setContentTitle("Scheduled Notification");
    builder.setContentText(content);
    builder.setSmallIcon(android.R.drawable.ic_dialog_alert);
    Log.e(ErrorTag, "finish get notificattion");
    return builder.build();
}
private void scheduleNotification(Notification notification, int delay) {
    Intent notificationIntent = new Intent(this, NoticationBroadcast.class);
    notificationIntent.putExtra(NoticationBroadcast.NOTIFICATION_ID, 1);
    notificationIntent.putExtra(NoticationBroadcast.NOTIFICATION, notification);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    Log.e(ErrorTag, "after getBroadcast");
    long futureInMillis = SystemClock.elapsedRealtime() + delay;
    Calendar calendar = Calendar.getInstance();
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    Log.e(ErrorTag, "after alarm");
}

接收器类

public class NoticationBroadcast extends BroadcastReceiver {
    public static String NOTIFICATION_ID = "notification-id";
    public static String NOTIFICATION = "notification";
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("ERROR", "beginning of on receive");
        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = intent.getParcelableExtra(NOTIFICATION);
        int id = intent.getIntExtra(NOTIFICATION_ID, 0);
        notificationManager.notify(id, notification);
        Log.e("ERROR", "end of on receive");
    }
}

编辑:

我更改了代码的行

alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

to

alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
            + (5000), pendingIntent);

它正常工作。但是我不知道为什么是这样。如果有人知道发生了什么,请告诉我。谢谢!

请参阅此处:

alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

您正在安排一项工作以便现在正好运行,这显然不会被解雇。

虽然在第二版(工作版本(中,您比现在安排了5000毫秒的作业:

alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                 + (5000), pendingIntent);

更改为预期的是:

alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() + 5000, 
                      pendingIntent);

最新更新