Merge pull request #1132 from Zahnstocher/remove_old_foreground
Remove old foreground workaround (API level <= 4)
This commit is contained in:
commit
1d6e45b66b
1 changed files with 3 additions and 73 deletions
|
@ -1,9 +1,5 @@
|
||||||
package net.osmand.plus;
|
package net.osmand.plus;
|
||||||
|
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
|
|
||||||
import net.osmand.PlatformUtil;
|
import net.osmand.PlatformUtil;
|
||||||
import net.osmand.access.AccessibleToast;
|
import net.osmand.access.AccessibleToast;
|
||||||
import android.app.AlarmManager;
|
import android.app.AlarmManager;
|
||||||
|
@ -56,35 +52,8 @@ public class NavigationService extends Service implements LocationListener {
|
||||||
private PendingIntent pendingIntent;
|
private PendingIntent pendingIntent;
|
||||||
private BroadcastReceiver broadcastReceiver;
|
private BroadcastReceiver broadcastReceiver;
|
||||||
private int usedBy = 0;
|
private int usedBy = 0;
|
||||||
|
|
||||||
|
|
||||||
private static Method mStartForeground;
|
|
||||||
private static Method mStopForeground;
|
|
||||||
private static Method mSetForeground;
|
|
||||||
private OsmAndLocationProvider locationProvider;
|
private OsmAndLocationProvider locationProvider;
|
||||||
|
|
||||||
private void checkForegroundAPI() {
|
|
||||||
// check new API
|
|
||||||
try {
|
|
||||||
mStartForeground = getClass().getMethod("startForeground", new Class[] {int.class, Notification.class});
|
|
||||||
mStopForeground = getClass().getMethod("stopForeground", new Class[] {boolean.class});
|
|
||||||
Log.d(PlatformUtil.TAG, "startForeground and stopForeground available");
|
|
||||||
} catch (NoSuchMethodException e) {
|
|
||||||
mStartForeground = null;
|
|
||||||
mStopForeground = null;
|
|
||||||
Log.d(PlatformUtil.TAG, "startForeground and stopForeground not available");
|
|
||||||
}
|
|
||||||
|
|
||||||
// check old API
|
|
||||||
try {
|
|
||||||
mSetForeground = getClass().getMethod("setForeground", new Class[] {boolean.class});
|
|
||||||
Log.d(PlatformUtil.TAG, "setForeground available");
|
|
||||||
} catch (NoSuchMethodException e) {
|
|
||||||
mSetForeground = null;
|
|
||||||
Log.d(PlatformUtil.TAG, "setForeground not available");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IBinder onBind(Intent intent) {
|
public IBinder onBind(Intent intent) {
|
||||||
return binder;
|
return binder;
|
||||||
|
@ -222,34 +191,14 @@ public class NavigationService extends Service implements LocationListener {
|
||||||
|
|
||||||
notification.setLatestEventInfo(this, Version.getAppName(cl) + " " + getString(R.string.osmand_service), nt,
|
notification.setLatestEventInfo(this, Version.getAppName(cl) + " " + getString(R.string.osmand_service), nt,
|
||||||
PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT));
|
PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT));
|
||||||
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
|
||||||
if (mStartForeground != null) {
|
startForeground(NOTIFICATION_SERVICE_ID, notification);
|
||||||
Log.d(PlatformUtil.TAG, "invoke startForeground");
|
|
||||||
try {
|
|
||||||
mStartForeground.invoke(this, NOTIFICATION_SERVICE_ID, notification);
|
|
||||||
} catch (InvocationTargetException e) {
|
|
||||||
Log.d(PlatformUtil.TAG, "invoke startForeground failed");
|
|
||||||
} catch (IllegalAccessException e) {
|
|
||||||
Log.d(PlatformUtil.TAG, "invoke startForeground failed");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Log.d(PlatformUtil.TAG, "invoke setForeground");
|
|
||||||
mNotificationManager.notify(NOTIFICATION_SERVICE_ID, notification);
|
|
||||||
try {
|
|
||||||
mSetForeground.invoke(this, Boolean.TRUE);
|
|
||||||
} catch (InvocationTargetException e) {
|
|
||||||
Log.d(PlatformUtil.TAG, "invoke setForeground failed");
|
|
||||||
} catch (IllegalAccessException e) {
|
|
||||||
Log.d(PlatformUtil.TAG, "invoke setForeground failed");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
// initializing variables
|
// initializing variables
|
||||||
checkForegroundAPI();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isContinuous(){
|
private boolean isContinuous(){
|
||||||
|
@ -287,26 +236,7 @@ public class NavigationService extends Service implements LocationListener {
|
||||||
broadcastReceiver = null;
|
broadcastReceiver = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mStopForeground != null) {
|
stopForeground(Boolean.TRUE);
|
||||||
Log.d(PlatformUtil.TAG, "invoke stopForeground");
|
|
||||||
try {
|
|
||||||
mStopForeground.invoke(this, Boolean.TRUE);
|
|
||||||
} catch (InvocationTargetException e) {
|
|
||||||
Log.d(PlatformUtil.TAG, "invoke stopForeground failed");
|
|
||||||
} catch (IllegalAccessException e) {
|
|
||||||
Log.d(PlatformUtil.TAG, "invoke stopForeground failed");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Log.d(PlatformUtil.TAG, "invoke setForeground");
|
|
||||||
try {
|
|
||||||
mSetForeground.invoke(this, Boolean.FALSE);
|
|
||||||
} catch (InvocationTargetException e) {
|
|
||||||
Log.d(PlatformUtil.TAG, "invoke setForeground failed");
|
|
||||||
} catch (IllegalAccessException e) {
|
|
||||||
Log.d(PlatformUtil.TAG, "invoke setForeground failed");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue