Set up Alarm to wake service (fix #175)
Этот коммит содержится в:
@@ -16,7 +16,9 @@
|
|||||||
package org.traccar.client;
|
package org.traccar.client;
|
||||||
|
|
||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
|
import android.app.AlarmManager;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
|
import android.app.PendingIntent;
|
||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
@@ -52,6 +54,9 @@ public class MainActivity extends PreferenceActivity implements OnSharedPreferen
|
|||||||
|
|
||||||
private SharedPreferences sharedPreferences;
|
private SharedPreferences sharedPreferences;
|
||||||
|
|
||||||
|
private AlarmManager alarmManager;
|
||||||
|
private PendingIntent alarmIntent;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
@@ -112,6 +117,9 @@ public class MainActivity extends PreferenceActivity implements OnSharedPreferen
|
|||||||
if (sharedPreferences.getBoolean(KEY_STATUS, false)) {
|
if (sharedPreferences.getBoolean(KEY_STATUS, false)) {
|
||||||
startTrackingService(true, false);
|
startTrackingService(true, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
|
||||||
|
alarmIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, AutostartReceiver.class), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeLauncherIcon() {
|
private void removeLauncherIcon() {
|
||||||
@@ -237,6 +245,8 @@ public class MainActivity extends PreferenceActivity implements OnSharedPreferen
|
|||||||
if (permission) {
|
if (permission) {
|
||||||
setPreferencesEnabled(false);
|
setPreferencesEnabled(false);
|
||||||
startService(new Intent(this, TrackingService.class));
|
startService(new Intent(this, TrackingService.class));
|
||||||
|
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
|
||||||
|
15000, 15000, alarmIntent);
|
||||||
} else {
|
} else {
|
||||||
sharedPreferences.edit().putBoolean(KEY_STATUS, false).commit();
|
sharedPreferences.edit().putBoolean(KEY_STATUS, false).commit();
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
|
||||||
@@ -250,6 +260,7 @@ public class MainActivity extends PreferenceActivity implements OnSharedPreferen
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void stopTrackingService() {
|
private void stopTrackingService() {
|
||||||
|
alarmManager.cancel(alarmIntent);
|
||||||
stopService(new Intent(this, TrackingService.class));
|
stopService(new Intent(this, TrackingService.class));
|
||||||
setPreferencesEnabled(true);
|
setPreferencesEnabled(true);
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user