AlarmManager 不断将 activity 带到前台
AlarmManager keeps bringing activity to foreground
我正在尝试编写将 运行 在后台调用服务器 API 的代码。谷歌搜索让我使用 AlarmManager,这是我的代码:
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 4000;
Intent alarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmIntent, 0);
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
这里的问题是,当我的应用程序在前台(当前在 android 主菜单或另一个应用程序中)时,每当警报触发时,它都会将我的 activity 发送到前面.
任何人都知道为什么会发生这种情况以及如何避免这种情况?我有一个由 AlarmReceiver class 启动的服务,我希望它在后台 运行。
AlarmReceiver.class:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, LogService.class);
i.putExtra("param1", "index.html");
i.putExtra("param2",
"http://www.vogella.com/index.html");
context.startService(i);
}
}
LogService.class
public class LogService extends IntentService{
public LogService() {
super("LogService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
String param1 = intent.getStringExtra("param1");
String param2 = intent.getStringExtra("param2");
Log.i("Hello from logservice!", "!!! -- " + param1 + " - " + param2);
}
}
添加清单内容:
<receiver android:name="com.example.app.alarm.AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name="com.example.app.alarm.LogService" >
</service>
提前致谢!
看了一圈之后,我发现自 API 19 年以来,所有重复的警报都不准确。了解到这一点后,我尝试使用 setRepeating 函数。我不知道为什么它的行为不同,但现在一切都按预期工作。
我正在尝试编写将 运行 在后台调用服务器 API 的代码。谷歌搜索让我使用 AlarmManager,这是我的代码:
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 4000;
Intent alarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmIntent, 0);
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
这里的问题是,当我的应用程序在前台(当前在 android 主菜单或另一个应用程序中)时,每当警报触发时,它都会将我的 activity 发送到前面.
任何人都知道为什么会发生这种情况以及如何避免这种情况?我有一个由 AlarmReceiver class 启动的服务,我希望它在后台 运行。
AlarmReceiver.class:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, LogService.class);
i.putExtra("param1", "index.html");
i.putExtra("param2",
"http://www.vogella.com/index.html");
context.startService(i);
}
}
LogService.class
public class LogService extends IntentService{
public LogService() {
super("LogService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
String param1 = intent.getStringExtra("param1");
String param2 = intent.getStringExtra("param2");
Log.i("Hello from logservice!", "!!! -- " + param1 + " - " + param2);
}
}
添加清单内容:
<receiver android:name="com.example.app.alarm.AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name="com.example.app.alarm.LogService" >
</service>
提前致谢!
看了一圈之后,我发现自 API 19 年以来,所有重复的警报都不准确。了解到这一点后,我尝试使用 setRepeating 函数。我不知道为什么它的行为不同,但现在一切都按预期工作。