Merge pull request #408 from Zahnstocher/patch-9
add support for new foreground API
This commit is contained in:
commit
e479626d18
1 changed files with 72 additions and 2 deletions
|
@ -30,6 +30,9 @@ import android.os.SystemClock;
|
|||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class NavigationService extends Service implements LocationListener {
|
||||
|
||||
public static class NavigationServiceBinder extends Binder {
|
||||
|
@ -61,6 +64,32 @@ public class NavigationService extends Service implements LocationListener {
|
|||
private LiveMonitoringHelper liveMonitoringHelper;
|
||||
private boolean startedForNavigation;
|
||||
|
||||
private static Method mStartForeground;
|
||||
private static Method mStopForeground;
|
||||
private static Method mSetForeground;
|
||||
|
||||
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
|
||||
public IBinder onBind(Intent intent) {
|
||||
return binder;
|
||||
|
@ -156,7 +185,27 @@ public class NavigationService extends Service implements LocationListener {
|
|||
notification.setLatestEventInfo(this, Version.getAppName(cl), getString(R.string.service_stop_background_service),
|
||||
PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT));
|
||||
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
||||
mNotificationManager.notify(NOTIFICATION_SERVICE_ID, notification);
|
||||
if (mStartForeground != null) {
|
||||
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");
|
||||
}
|
||||
}
|
||||
// }
|
||||
return START_REDELIVER_INTENT;
|
||||
}
|
||||
|
@ -165,7 +214,7 @@ public class NavigationService extends Service implements LocationListener {
|
|||
public void onCreate() {
|
||||
super.onCreate();
|
||||
// initializing variables
|
||||
setForeground(true);
|
||||
checkForegroundAPI();
|
||||
}
|
||||
|
||||
private boolean isContinuous(){
|
||||
|
@ -198,6 +247,27 @@ public class NavigationService extends Service implements LocationListener {
|
|||
unregisterReceiver(broadcastReceiver);
|
||||
broadcastReceiver = null;
|
||||
}
|
||||
|
||||
if (mStopForeground != null) {
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue