This is becoming unreal how much an alarm will not work. I am having 2 issues with Android killing my app and as such everything stops even though everything i read says it should work.
My AlarmManager item dies when i Greenify my app, Greenify to me is same as letting phone sit there and have Android kill it. Either way, I should have an alarm in the cue waiting to fire my PwendingIntent. Otherwise, what good is RTC_WAKEUP for an alarm setting.
My Manifest
<receiver
android:name=".ContactAlarmReceiver"
android:enabled="false"
android:process=":alarmremote">
<intent-filter>
<action android:name="com.example.johnbravado.zionwork.CONTACTALARM" />
<action android:name="com.example.johnbravado.zionwork.NOTIFLTBTN" />
<action android:name="com.example.johnbravado.zionwork.NOTIFRTBTN" />
</intent-filter>
</receiver>
My Receiver
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//throw new UnsupportedOperationException("Not yet implemented");
//Toast.makeText(context, "Received Alarm", Toast.LENGTH_SHORT).show();
Log.d("johnbravadoCAR ","received");
PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"com.example.johnbravado.zionwork");
wakeLock.acquire();
ComponentName component = new ComponentName(context, ContactAlarmIntentService.class);
context.getPackageManager()
.setComponentEnabledSetting(component,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Intent alarmIntent = new Intent(context, ContactAlarmIntentService.class);
alarmIntent.putExtras(intent.getExtras());
alarmIntent.setAction(intent.getAction());
context.startService(alarmIntent);
wakeLock.release();
}
I know I have android:enabled="false"
in the manifest. I counter that with a PackageManager call before i set alarm. See below
private void setAlarm() {
//ContactAlarmReceiver contactAlarmReceiver = new ContactAlarmReceiver();
//contactAlarmReceiver.setAlarm(getApplicationContext(), dateInMillis, phonenumberET.getText().toString(), firstnameET.getText().toString());
ComponentName component = new ComponentName(this, ContactAlarmReceiver.class);
getPackageManager()
.setComponentEnabledSetting(component,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(MyConstants.BROADCAST_ACTION_CONTACT_ALARM);
alarmIntent.putExtra("phone", phonenumberET.getText().toString());
alarmIntent.putExtra("name", firstnameET.getText().toString());
PendingIntent pi = PendingIntent.getBroadcast(this, 123456, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= 23) {
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
} else if (Build.VERSION.SDK_INT >= 19) {
am.setExact(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
} else {
am.set(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
}
}
Once I set an alarm for a time longer then 30 minutes later or if I set an alarm for 3 minutes later and Greenify my app, i do not see the Log.d print out in my logcat viewer. AlarmManager, from what all i can read and understand, should fire to my BroadcastReceiver and do work.
If i ever get this to work i am going to write a detailed explanation so someone else does not go through this same thing. If this shoudl work, is there some setting that may be killing associations with my app i need to uncheck?
Update:
This code is solid. I was having an issue with a secondary application, Greenify, which got turned on somehow apart from my direct interaction. That app was killing my app when the screen went off. After telling Greenify to not touch my app, this code performed as necessary. On the bright side, I learned a lot about BroadcastReceiver, AlarmManager and Service implementation since I tried like 10 different ways to battle a problem i could not solve with code.
Greenify was killing my application. See update in OP