I have a location service that runs when user clicks a button and applies th location.
However, it gets killed after about a minute. I've tested it on android 8/9. I've read some other answers on here about it but they haven't helped, but my knowledge on intents and broadcasts isn't good.
LocationService.java
public class LocationService extends Service {
MockLocationProvider mockNetwork;
MockLocationProvider mockGps;
private static final int NOTIFICATION = 1;
static NotificationManager notificationManager;
Context context;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//TODO do something useful
context = getApplicationContext();
pushLocation(intent);
return LocationService.START_STICKY;
}
@Override
public void onDestroy() {
shutdown();
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void pushLocation(Intent intent) {
try {
if (intent.hasExtra("lat") && intent.hasExtra("lng") ) {
double lat = intent.getDoubleExtra("lat", 45);
double lng = intent.getDoubleExtra("lng", 45);
mockNetwork = new MockLocationProvider(LocationManager.NETWORK_PROVIDER, context);
mockGps = new MockLocationProvider(LocationManager.GPS_PROVIDER, context);
mockNetwork.pushLocation(lat, lng);
mockGps.pushLocation(lat, lng);
setNotification(lat, lng);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void setNotification(Double mLat, Double mLng) {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel = new NotificationChannel("ID", "Name", importance);
notificationManager.createNotificationChannel(notificationChannel);
builder = new NotificationCompat.Builder(context, notificationChannel.getId());
} else {
builder = new NotificationCompat.Builder(context);
}
//open MainActivity when clicked
pendingIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP),
0);
//action when notification button clicked
Intent intentAction = new Intent(context, ActionReceiver.class);
intentAction.putExtra("action","actionNotification");
pendingCloseIntent = PendingIntent.getBroadcast(context,0, intentAction, PendingIntent.FLAG_UPDATE_CURRENT);
//build notification
builder = builder
.setSmallIcon(R.drawable.ic_logo)
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setContentTitle(getString(R.string.app_name) + " is running...")
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentText(mLat + ", " + mLng)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setTicker(getString(R.string.app_name) + " is running...") //accessibility
.addAction(android.R.drawable.ic_menu_close_clear_cancel, "STOP", pendingCloseIntent)
.setOngoing(true);
notificationManager.notify(NOTIFICATION, builder.build());
}
public void shutdown() {
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION);
if (mockNetwork != null)
mockNetwork.shutdown();
if (mockGps != null)
mockGps.shutdown();
}
}
ActionReceiver.java
public class ActionReceiver extends BroadcastReceiver {
Context context;
MockLocationProvider mockNetwork;
MockLocationProvider mockGps;
private static final int NOTIFICATION = 1;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getStringExtra("action");
if(action.equals("actionNotification")){
mockNetwork = new MockLocationProvider(LocationManager.NETWORK_PROVIDER, context);
mockGps = new MockLocationProvider(LocationManager.GPS_PROVIDER, context);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION);
try {
if (mockNetwork != null)
mockNetwork.shutdown();
if (mockGps != null)
mockGps.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
These are registered in my AndroidManifest with permissions:
...
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
<receiver android:name="com.package.locationtest.ActionReceiver" />
<service
android:name=".LocationService"
android:label="Service">
</service>
I don't understand the problem here. Can anyone help?