-1

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.

John Bravado
  • 137
  • 4
  • 10
  • If your app is killed, when the alarm goes off Android will see that the receiver is not enabled. The enable won't last past the application lifecycle. So it can't go through. Try enabling it and see if that's the only issue. – Gabe Sechan Jan 16 '17 at 22:23
  • i have tried to enable the receiver and it still does not get received – John Bravado Jan 16 '17 at 22:23
  • And what does Greenify do *exactly*? If it puts the app into force stop state no receivers or services are allowed to fire. – Gabe Sechan Jan 16 '17 at 22:24
  • I do not know what it does exactly but I get same effect if i set alarm for 30 minutes later, Android kills my app. i figure android death and greenify death may be on the same lines. – John Bravado Jan 16 '17 at 22:27
  • I wouldn't assume that at all. Android is just killing the process. I have no clue what Greenify does. In fact if saving battery is the idea I wouldn't be surprised if a force stop is exactly what it does, to make sure services stay dead – Gabe Sechan Jan 16 '17 at 22:28
  • Please try passing "getApplicationContext()" instead of "this" in PandingIntent.getBroadcast(... Also you may try instantiating an explicit Intent: Intent(Context packageContext, Class> cls) – Tassadar Jan 16 '17 at 22:29
  • I tried using getApplicationContext() and it still did not fire after i greenified the App. the other test i have to wait 30 minutes for to let android kill the app for memory – John Bravado Jan 16 '17 at 22:33
  • 1
    The Play store listing for Greenify says: "Greenifying an app implies that you are aware that all the background functionality (service, periodic task, event receiver, alarm, widget update, push message) of this app will become out of service during the hibernation except when you are using this app." It seems this is exactly what you are experiencing. I don't think its reasonable to make your app work in this circumstance; the extra effort only matters to users who use Greenify and who hibernate your app with it, which hopefully should be a very small number. – Karakuri Jan 16 '17 at 22:47
  • I haven heard before about Greenify, what i usually do to "replicate" the case when the app is killed by the os to free memory is to open multiple apps until it gets killed, i know when it happens when its shown as "(dead)" in the processes filter in android monitor. Please take also as a consideration that when the device is rebooted all alarms are lost. – Tassadar Jan 16 '17 at 22:47
  • Thank you Karakuri, however i just now started teasting with Greenify. so take that out of the equation. for the past 3 days i have been trying and sitting around for 30 minutes to see if alarm goes off. I was hoping greenify would speed that process up. But apparently it will not. – John Bravado Jan 16 '17 at 22:50
  • Tassadar is that in android studio somewhere? How can i see. That right there is what i am lacking i dont really know when Android kills it. – John Bravado Jan 16 '17 at 22:53
  • Yes, in android studio open Android Monitor tab (6), in the upper left there's the device filter, on the right is the process filter, this will show your app package, open other several apps that demand memory, maybe one game will be enought, when your app is killed by the os it shows "[DEAD]" on the right side of the package name – Tassadar Jan 16 '17 at 22:58
  • Karakuri, you did make me think though. i have never explicitly told greenify to greenify my app automatically. but it may have been so i put it on a block from greenify so greenify wont touch it. and set an alarm for later. – John Bravado Jan 16 '17 at 23:04

1 Answers1

0

Greenify was killing my application. See update in OP

John Bravado
  • 137
  • 4
  • 10