我正在尝试实现一个简单的服务,它每小时将用户的当前位置发送到服务器。我的应用程序只有登录Activity
和设置活动,通常在后台工作。我用AlarmManager
它来实现我的目的,它运行良好,但是receiver
当我被迫停止我的应用程序时我的不起作用。这是我的代码。
我的应用程序.java
@Override
public void onCreate() {
super.onCreate();
startService();
}
private void startService(){
Intent receiver = new Intent(this, LocationAlaramReceiver.class);
PendingIntent intent = PendingIntent.getBroadcast(this, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
mgr.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), AlarmManager.INTERVAL_HOUR, intent);
}
AndroidManifest.xml
<receiver
android:name=".receiver.LocationAlarmReceiver">
</receiver>
LocationAlarmReceiver.java
public class LocationAlarmReceiver extends BroadcastReceiver
{
private final static String TAG = "LocationAlarmReceiver";
@Override
public void onReceive(Context context, Intent intent)
{
Log.d(TAG, "=========== onReceive ==============");
//Do something later
}
}
只是看到我的情况,它看起来并不保证AlarmManager
总是会被调用。我想继续LocationAlramReceiver
工作每个小时。我该如何解决我的问题?