I am using an Alarm manager to call a notification on a receiver method. I put some debug code in it, and found that the process didn't even get in there.
Here are my codes:
Manifest.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>
Menu.java (the main class)
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");
}
Receiver class
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");
}
}
Edit:
I changed a line of the code
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
to
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (5000), pendingIntent);
It is working perfectly. But I don't know why is it. If anyone has know what happened, please tell me. Thanks!
See here:
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
You are scheduling a job to run exactly now, which, obviously, won't be fired.
Whereas in the second version (working version), you are scheduling a job 5000 ms later than now:
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (5000), pendingIntent);
Changing to this would work as expected:
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() + 5000,
pendingIntent);