由于我使用基于时间间隔的定位服务调用定位服务,但是当 onDestroy() 被调用时,所有服务都被停止,即使应用程序重新启动,位置也没有被获取并且警报管理器无法正常工作。
使用 GPS 位置提供程序。 使用 mqtt mosquitto 代理将位置发送到服务器。
public void onCreate() {
super.onCreate();
Log.i(TAG, "Inside onCreate() Method...............................");
mLog.info( "Inside onCreate() Method...............................");
prefManager = new PrefManager(getApplicationContext());
mqttParameters = MqttParams.getSetInstance();
interval = GpsInterval.getSetInstance();
Log.i(TAG, "mqttParameters : " + mqttParameters.toString());
Log.i(TAG, "isPunchIn : " + isPunchIn);
Log.i(TAG, "interval : " + interval);
mLog.info( "mqttParameters : " + mqttParameters.toString());
mLog.info( "isPunchIn : " + isPunchIn);
mLog.info( "interval : " + interval);
try {
if (Build.VERSION.SDK_INT >= 26) {
String CHANNEL_ID = "my_service";
String CHANNEL_NAME = "My Background Service";
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
CHANNEL_NAME, NotificationManager.IMPORTANCE_NONE);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setCategory(Notification.CATEGORY_SERVICE).setPriority(PRIORITY_MIN).build();
startForeground(101, notification);
}
// initialize();
} catch (Exception e) {
e.printStackTrace();
}
}
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "Inside onStartCommand Method. . . . .. . . .. ");
mLog.info( "Inside onStartCommand Method. . . . .. . . .. ");
mSharedPreference = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
if (intent != null) {
//Once location service started set App status as true
SharedPreferences.Editor editor = mSharedPreference.edit();
editor.putBoolean(SharedPrefConstants.SHP_BOOLEAN_APP_STATUS, true);
editor.commit();
String action = intent.getAction();
Bundle extras = intent.getExtras();
Log.i(TAG, "Action " + action);
Log.i(TAG, "extras " + extras);
mLog.info( "Action " + action);
mLog.info( "extras " + extras);
if (extras != null) {
boolean isTimeTampered = extras.getBoolean(
SharedPrefConstants.IS_TIME_TAMPERED, false);
boolean isForcePunchOut = extras.getBoolean(
Constants.ACTION_FORCE_PUNCH_OUT, false);
boolean isNeedToStopTrack = extras.getBoolean(
SharedPrefConstants.NEED_TO_STOP_TRACKING, false);
Log.i(TAG, "isTimeTampered " + isTimeTampered);
Log.i(TAG, "isForcePunchOut " + isForcePunchOut);
mLog.info( "isTimeTampered " + isTimeTampered);
mLog.info( "isForcePunchOut " + isForcePunchOut);
if (isTimeTampered == true || isForcePunchOut == true || isNeedToStopTrack) {
Log.i(TAG, "Time is tampered |Force punch out stopLocationTracking");
mLog.info( "Time is tampered |Force punch out stopLocationTracking");
stopLocationTracking();
stopSelf();
// DataSync.uploadLocationData(mContext);
}
}//todo 26/2/2020
else {
//set alarm for periodic sending
if (interval.getInterval() != null)
Log.i(TAG, "onStartCommand: interval.getInterval() "+interval.getInterval());
ReceiverCall.setAlarm(false, getApplicationContext(), interval.getInterval());
// ReceiverCall.setAlarm(false, getApplicationContext(), 1); //todo 16/12/2020
//call method to capture locations
captureCurrentLocation(0, getApplicationContext());
//retry location if location from all the providers is null
if (currentLocation.getGpsLocation() == null &&
currentLocation.getNetworkLocation() == null &&
currentLocation.getFusedLocation() == null) {
Log.i(TAG, "Inside retry location................");
captureCurrentLocation(0, getApplicationContext());
}
}
}
return Service.START_NOT_STICKY;
}
@Override
public void onDestroy() {
Log.i(TAG, "onDestroy. . . . .. . . .. ");
super.onDestroy();
stopSelf();
ReceiverCall.cancelAlarm(getApplicationContext());
disconnect();
}