private static final Class<?>[] mSetForegroundSignature = new Class[] {
boolean.class};
private static final Class<?>[] mStartForegroundSignature = new Class[] {
int.class, Notification.class};
private static final Class<?>[] mStopForegroundSignature = new Class[] {
boolean.class}; private NotificationManager mNM;
private Method mSetForeground;
private Method mStartForeground;
private Method mStopForeground;
private Object[] mSetForegroundArgs = new Object[1];
private Object[] mStartForegroundArgs = new Object[2];
private Object[] mStopForegroundArgs = new Object[1]; void invokeMethod(Method method, Object[] args) {
try {
method.invoke(this, args);
} catch (InvocationTargetException e) {
// Should not happen.
Log.w("ApiDemos", "Unable to invoke method", e);
} catch (IllegalAccessException e) {
// Should not happen.
Log.w("ApiDemos", "Unable to invoke method", e);
}
} /**
* This is a wrapper around the new startForeground method, using the older
* APIs if it is not available.
*/
void startForegroundCompat(int id, Notification notification) {
// If we have the new startForeground API, then use it.
if (mStartForeground != null) {
mStartForegroundArgs[0] = Integer.valueOf(id);
mStartForegroundArgs[1] = notification;
invokeMethod(mStartForeground, mStartForegroundArgs);
return;
} // Fall back on the old API.
mSetForegroundArgs[0] = Boolean.TRUE;
invokeMethod(mSetForeground, mSetForegroundArgs);
mNM.notify(id, notification);
} /**
* This is a wrapper around the new stopForeground method, using the older
* APIs if it is not available.
*/
void stopForegroundCompat(int id) {
// If we have the new stopForeground API, then use it.
if (mStopForeground != null) {
mStopForegroundArgs[0] = Boolean.TRUE;
invokeMethod(mStopForeground, mStopForegroundArgs);
return;
} // Fall back on the old API. Note to cancel BEFORE changing the
// foreground state, since we could be killed at that point.
mNM.cancel(id);
mSetForegroundArgs[0] = Boolean.FALSE;
invokeMethod(mSetForeground, mSetForegroundArgs);
} @Override
public void onCreate() {
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
try {
mStartForeground = getClass().getMethod("startForeground",
mStartForegroundSignature);
mStopForeground = getClass().getMethod("stopForeground",
mStopForegroundSignature);
return;
} catch (NoSuchMethodException e) {
// Running on an older platform.
mStartForeground = mStopForeground = null;
}
try {
mSetForeground = getClass().getMethod("setForeground",
mSetForegroundSignature);
} catch (NoSuchMethodException e) {
throw new IllegalStateException(
"OS doesn't have Service.startForeground OR Service.setForeground!");
}
} @Override
public void onDestroy() {
// Make sure our notification is gone.
stopForegroundCompat(R.string.foreground_service_started);
}

摘自:http://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)

最新文章

  1. 1 python大数据挖掘系列之基础知识入门
  2. CS0103: The name ‘Scripts’ does not exist in the current context解决方法
  3. iOS Crash日志
  4. 关于兼容性——百分比对于IE浏览器的影响
  5. python while 循环语句
  6. github page 配置hexo 博客 的常见错误
  7. 内存管理中提到的hot cold page
  8. 深入浅出mybatis之映射器
  9. 以太坊中的Ghost协议
  10. Java常用系统变量收集
  11. JavaScript学习(2)call&amp;apply&amp;bind&amp;eval用法
  12. 【精】C语言之变量存储类型
  13. 列名 &#39;Discriminator&#39; 无效 解决方案
  14. 打印实例对象的名字 默认调用父类的toString 可重写
  15. libsvm处理多分类的问题
  16. BZOJ3997 TJOI2015组合数学(动态规划)
  17. BZOJ4869 六省联考2017相逢是问候(线段树+欧拉函数)
  18. 【转载】Python测试框架doctest
  19. python基础02—运算符与流程控制
  20. IntelliJ IDEA强制更新Maven的包

热门文章

  1. 安卓高级9 用原生intent分享
  2. Bootstrap3 栅格系统-媒体查询
  3. SQLite 数据类型(http://www.w3cschool.cc/sqlite/sqlite-data-types.html)
  4. 阻塞IO服务器模型之多线程服务器模型
  5. Android 玩转IOC,Retfotit源码解析,教你徒手实现自定义的Retrofit框架
  6. 假设一个大小为100亿个数据的数组,该数组是从小到大排好序的,现在该数组分成若干段,每个段的数据长度小于20「也就是说:题目并没有说每段数据的size 相同,只是说每个段的 size &lt; 20 而已」
  7. 带你深入理解STL之Deque容器
  8. Android水印相机
  9. Android系统剪切板
  10. Android简易实战教程--第十五话《在外部存储中读写文件》