AlarmManager Does not work when Activity in background

孤者浪人 提交于 2020-01-16 18:35:32

问题


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

        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 < 0) diff = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
    am.setInexactRepeating(AlarmManager.RTC_WAKEUP, diff, AlarmManager.INTERVAL_DAY, sender);

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 -

<receiver android:name="MainActivity$AlarmReceiver" android:enabled="true" />

The manifest is

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="8" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.yachtdroid.ba.MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleInstance" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.yachtdroid.ba.ui.Settings" >
    </activity>
    <receiver android:name="MainActivity$ChargingOnReceiver" android:enabled="true">
        <intent-filter>
        <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
        <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
    </intent-filter>
    </receiver>
    <service android:name="com.yachtdroid.ba.services.MailSenderService" />
    <service android:name="com.yachtdroid.ba.services.SmsSender" />

    <activity android:name="com.yachtdroid.ba.ui.YDSiteView" />
    <activity
        android:name="com.yachtdroid.ba.ui.BatteryAlertDialog"
        android:theme="@style/NoTitleDialog" />
    <activity
        android:name="com.yachtdroid.ba.ui.ActivityLog"
        android:theme="@style/NoTitleDialog" />
    <receiver android:name="MainActivity$AlarmReceiver" android:enabled="true" />
        <!-- intent-filter>
            <action android:name="alarm_action"/>
        </intent-filter>
    </receiver -->
</application>

The receiver code is

    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();
        }
    }
}

How it can be solved? Thanks, Eyal.


回答1:


Try to remove the code for the broadcast receiver from the Activity ,make another java file and put it there and also modify your manifest as per that

        <receiver android:name=".AlarmReceiver" />

instead of

<receiver android:name="MainActivity$AlarmReceiver" android:enabled="true" />


来源:https://stackoverflow.com/questions/20080531/alarmmanager-does-not-work-when-activity-in-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!