Note that there are some explanatory texts on larger screens.

plurals
  1. POAlarmManager Does not work when Activity in background
    primarykey
    data
    text
    <p>In my App, I'm using AlarmManager for transmitting keep-alive every day once a day. I'm using the following code for starting athe AlaramManager</p> <pre><code> Calendar calendar = DateUtils .getNextKeepAliveTime(keepAliveHours, keepAliveMinutes); Intent intent = new Intent(this, AlarmReceiver.class); PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), ALARM_REQUEST_CODE, intent, 0); // PendingIntent.FLAG_UPDATE_CURRENT); // Get the AlarmManager service Log.d(TAG, "cal: " + calendar.toString()); AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); long diff = calendar.getTimeInMillis() - System.currentTimeMillis(); if(diff &lt; 0) diff = AlarmManager.INTERVAL_FIFTEEN_MINUTES; am.setInexactRepeating(AlarmManager.RTC_WAKEUP, diff, AlarmManager.INTERVAL_DAY, sender); </code></pre> <p>When activity goes background, the receiver, AlarmReceiver, does not get any event. But it working successfully when activity in foreground. Ther receiver declaration in my manifest is -</p> <pre><code>&lt;receiver android:name="MainActivity$AlarmReceiver" android:enabled="true" /&gt; </code></pre> <p>The manifest is </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; </code></pre> <p></p> <pre><code>&lt;uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" /&gt; &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt; &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt; &lt;uses-permission android:name="android.permission.SEND_SMS" /&gt; &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt; &lt;uses-permission android:name="android.permission.INTERNET" /&gt; &lt;application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" &gt; &lt;activity android:name="com.yachtdroid.ba.MainActivity" android:label="@string/app_name" android:launchMode="singleInstance" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;activity android:name="com.yachtdroid.ba.ui.Settings" &gt; &lt;/activity&gt; &lt;receiver android:name="MainActivity$ChargingOnReceiver" android:enabled="true"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.ACTION_POWER_CONNECTED" /&gt; &lt;action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; &lt;service android:name="com.yachtdroid.ba.services.MailSenderService" /&gt; &lt;service android:name="com.yachtdroid.ba.services.SmsSender" /&gt; &lt;activity android:name="com.yachtdroid.ba.ui.YDSiteView" /&gt; &lt;activity android:name="com.yachtdroid.ba.ui.BatteryAlertDialog" android:theme="@style/NoTitleDialog" /&gt; &lt;activity android:name="com.yachtdroid.ba.ui.ActivityLog" android:theme="@style/NoTitleDialog" /&gt; &lt;receiver android:name="MainActivity$AlarmReceiver" android:enabled="true" /&gt; &lt;!-- intent-filter&gt; &lt;action android:name="alarm_action"/&gt; &lt;/intent-filter&gt; &lt;/receiver --&gt; &lt;/application&gt; </code></pre> <p></p> <p>The receiver code is </p> <pre><code> public static class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, intent.getExtras().toString()); if (!inForeground) { Log.d(TAG, "*** Move app to front!"); Intent it = new Intent("intent.my.action"); it.setComponent(new ComponentName(context.getPackageName(), MainActivity.class.getName())); it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); context.getApplicationContext().startActivity(it); } try { Toast.makeText(context, "alarm message", Toast.LENGTH_SHORT).show(); MainActivity.instance.sendKeepALive(); } catch (Exception e) { Toast.makeText( context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } } </code></pre> <p>How it can be solved? Thanks, Eyal.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload