AlarmManager不工作时在后台活动时 [英] AlarmManager Does not work when Activity in background
问题描述
在我的应用程序,我使用AlarmManager用于发送保持活着每一天,每天一次。
我用下面的code,起athe AlaramManager
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);
当活动变为背景,接收机,AlarmReceiver,没有得到任何事件。但是,在前台工作的成功时的活动。
在我的清单疗法接收机声明 -
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" />
该清单是
<?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>
接收器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();
}
}
}
如何能解决呢?
谢谢,
埃亚尔。
How it can be solved? Thanks, Eyal.
推荐答案
尝试删除code从活动广播接收器,使其他Java文件,并把它放在那里,也修改表现为占该
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" />
而不是
<receiver android:name="MainActivity$AlarmReceiver" android:enabled="true" />
这篇关于AlarmManager不工作时在后台活动时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!