1
0
mirror of https://github.com/moparisthebest/Yaaic synced 2025-01-08 12:18:07 -05:00

IRCService: Use reflection to call setForeground() on old versions.

This commit is contained in:
Sebastian Kaspari 2012-01-21 12:59:02 +01:00
parent d4f2c1cc3a
commit 46a45523a8

View File

@ -70,6 +70,8 @@ public class IRCService extends Service
private static final Class[] mStartForegroundSignature = new Class[] { int.class, Notification.class };
@SuppressWarnings("rawtypes")
private static final Class[] mStopForegroundSignature = new Class[] { boolean.class };
@SuppressWarnings("rawtypes")
private static final Class[] mSetForegroudSignaure = new Class[] { boolean.class };
public static final String ACTION_FOREGROUND = "org.yaaic.service.foreground";
public static final String ACTION_BACKGROUND = "org.yaaic.service.background";
@ -162,6 +164,7 @@ public class IRCService extends Service
* @param startId
* @return
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
if (intent != null) {
@ -358,7 +361,17 @@ public class IRCService extends Service
}
} else {
// Fall back on the old API.
setForeground(true);
try {
Method setForeground = getClass().getMethod("setForeground", mSetForegroudSignaure);
setForeground.invoke(this, new Object[] { true });
} catch (NoSuchMethodException exception) {
// Should not happen
} catch (InvocationTargetException e) {
// Should not happen.
} catch (IllegalAccessException e) {
// Should not happen.
}
notificationManager.notify(id, notification);
}
}
@ -385,7 +398,17 @@ public class IRCService extends Service
// Fall back on the old API. Note to cancel BEFORE changing the
// foreground state, since we could be killed at that point.
notificationManager.cancel(id);
setForeground(false);
try {
Method setForeground = getClass().getMethod("setForeground", mSetForegroudSignaure);
setForeground.invoke(this, new Object[] { true });
} catch (NoSuchMethodException exception) {
// Should not happen
} catch (InvocationTargetException e) {
// Should not happen.
} catch (IllegalAccessException e) {
// Should not happen.
}
}
}