我是靠谱客的博主 细心篮球,最近开发中收集的这篇文章主要介绍Android创建前台运行的Service,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

转自http://blog.csdn.net/ameyume/article/details/9150755

 

 

Service如果要防止尽可能不被系统杀掉,需要设置为在前台运行。

由于设置前台运行service的方法在2.0之前和2.0之后有所变化。

所以需要根据不同的版本进行区分;或者完全使用反射机制来处理,这样只要有相应的方法就可以使用,否则使用其他版本的方法。

下面是一个设置servcie前台运行的例子,参考了API中对Service的说明。

http://developer.android.com/reference/android/app/Service.html#

[java] view plain copy print ?
  1. import java.lang.reflect.InvocationTargetException; 
  2. import java.lang.reflect.Method; 
  3.  
  4. import android.app.Notification; 
  5. import android.app.NotificationManager; 
  6. import android.app.PendingIntent; 
  7. import android.app.Service; 
  8. import android.content.Context; 
  9. import android.content.Intent; 
  10. import android.os.IBinder; 
  11. import android.os.Build.VERSION; 
  12. import android.util.Log; 
  13.  
  14. public class ForegroundService extends Service { 
  15.     private static final String TAG = "ForegroundService"
  16.      
  17.     private boolean mReflectFlg = false
  18.      
  19.     private static final int NOTIFICATION_ID = 1; // 如果id设置为0,会导致不能设置为前台service 
  20.     private static final Class<?>[] mSetForegroundSignature = new Class[] { 
  21.         boolean.class}; 
  22.     private static final Class<?>[] mStartForegroundSignature = new Class[] { 
  23.         int.class, Notification.class}; 
  24.     private static final Class<?>[] mStopForegroundSignature = new Class[] { 
  25.         boolean.class}; 
  26.  
  27.     private NotificationManager mNM;   
  28.     private Method mSetForeground; 
  29.     private Method mStartForeground;   
  30.     private Method mStopForeground; 
  31.     private Object[] mSetForegroundArgs = new Object[1]; 
  32.     private Object[] mStartForegroundArgs = new Object[2];   
  33.     private Object[] mStopForegroundArgs = new Object[1];   
  34.      
  35.     @Override   
  36.     public void onCreate() {   
  37.         super.onCreate(); 
  38.         Log.d(TAG, "onCreate"); 
  39.          
  40.         mNM = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);   
  41.         try {   
  42.             mStartForeground = ForegroundService.class.getMethod("startForeground", mStartForegroundSignature);   
  43.             mStopForeground = ForegroundService.class.getMethod("stopForeground", mStopForegroundSignature);   
  44.         } catch (NoSuchMethodException e) {   
  45.             mStartForeground = mStopForeground = null;   
  46.         }   
  47.          
  48.         try
  49.             mSetForeground = getClass().getMethod("setForeground"
  50.                     mSetForegroundSignature); 
  51.         } catch (NoSuchMethodException e) { 
  52.             throw new IllegalStateException( 
  53.                     "OS doesn't have Service.startForeground OR Service.setForeground!"); 
  54.         } 
  55.  
  56.         Notification.Builder builder = new Notification.Builder(this); 
  57.         PendingIntent contentIntent = PendingIntent.getActivity(this, 0,   
  58.                 new Intent(this, MainActivity.class), 0);   
  59.         builder.setContentIntent(contentIntent); 
  60.         builder.setSmallIcon(R.drawable.icon); 
  61.         builder.setTicker("Foreground Service Start"); 
  62.         builder.setContentTitle("Foreground Service"); 
  63.         builder.setContentText("Make this service run in the foreground."); 
  64.         Notification notification = builder.build(); 
  65.          
  66.         startForegroundCompat(NOTIFICATION_ID, notification);   
  67.     }   
  68.      
  69.     @Override 
  70.     public int onStartCommand(Intent intent, int flags, int startId) { 
  71.         super.onStartCommand(intent, flags, startId); 
  72.         Log.d(TAG, "onStartCommand"); 
  73.          
  74.         return START_STICKY; 
  75.     }  
  76.      
  77.     @Override   
  78.     public IBinder onBind(Intent intent) {   
  79.         return null;   
  80.     }   
  81.  
  82.     @Override   
  83.     public void onDestroy() {   
  84.         super.onDestroy(); 
  85.         Log.d(TAG, "onDestroy"); 
  86.          
  87.         stopForegroundCompat(NOTIFICATION_ID);   
  88.     } 
  89.      
  90.     void invokeMethod(Method method, Object[] args) { 
  91.         try
  92.             method.invoke(this, args); 
  93.         } catch (InvocationTargetException e) { 
  94.             // Should not happen. 
  95.             Log.w("ApiDemos", "Unable to invoke method", e); 
  96.         } catch (IllegalAccessException e) { 
  97.             // Should not happen. 
  98.             Log.w("ApiDemos", "Unable to invoke method", e); 
  99.         } 
  100.     } 
  101.      
  102.     /**
  103.      * This is a wrapper around the new startForeground method, using the older
  104.      * APIs if it is not available.
  105.      */ 
  106.     void startForegroundCompat(int id, Notification notification) { 
  107.         if (mReflectFlg) { 
  108.             // If we have the new startForeground API, then use it. 
  109.             if (mStartForeground != null) { 
  110.                 mStartForegroundArgs[0] = Integer.valueOf(id); 
  111.                 mStartForegroundArgs[1] = notification; 
  112.                 invokeMethod(mStartForeground, mStartForegroundArgs); 
  113.                 return
  114.             } 
  115.      
  116.             // Fall back on the old API. 
  117.             mSetForegroundArgs[0] = Boolean.TRUE; 
  118.             invokeMethod(mSetForeground, mSetForegroundArgs); 
  119.             mNM.notify(id, notification); 
  120.         } else
  121.             /* 还可以使用以下方法,当sdk大于等于5时,调用sdk现有的方法startForeground设置前台运行,
  122.              * 否则调用反射取得的sdk level 5(对应Android 2.0)以下才有的旧方法setForeground设置前台运行 */ 
  123.              
  124.             if(VERSION.SDK_INT >= 5) { 
  125.                 startForeground(id, notification); 
  126.             } else
  127.                 // Fall back on the old API. 
  128.                 mSetForegroundArgs[0] = Boolean.TRUE; 
  129.                 invokeMethod(mSetForeground, mSetForegroundArgs); 
  130.                 mNM.notify(id, notification);     
  131.             } 
  132.         } 
  133.     } 
  134.  
  135.     /**
  136.      * This is a wrapper around the new stopForeground method, using the older
  137.      * APIs if it is not available.
  138.      */ 
  139.     void stopForegroundCompat(int id) { 
  140.         if (mReflectFlg) { 
  141.             // If we have the new stopForeground API, then use it. 
  142.             if (mStopForeground != null) { 
  143.                 mStopForegroundArgs[0] = Boolean.TRUE; 
  144.                 invokeMethod(mStopForeground, mStopForegroundArgs); 
  145.                 return
  146.             } 
  147.      
  148.             // Fall back on the old API.  Note to cancel BEFORE changing the 
  149.             // foreground state, since we could be killed at that point. 
  150.             mNM.cancel(id); 
  151.             mSetForegroundArgs[0] = Boolean.FALSE; 
  152.             invokeMethod(mSetForeground, mSetForegroundArgs); 
  153.         } else
  154.             /* 还可以使用以下方法,当sdk大于等于5时,调用sdk现有的方法stopForeground停止前台运行,
  155.              * 否则调用反射取得的sdk level 5(对应Android 2.0)以下才有的旧方法setForeground停止前台运行 */ 
  156.              
  157.             if(VERSION.SDK_INT >= 5) { 
  158.                 stopForeground(true); 
  159.             } else
  160.                 // Fall back on the old API.  Note to cancel BEFORE changing the 
  161.                 // foreground state, since we could be killed at that point. 
  162.                 mNM.cancel(id); 
  163.                 mSetForegroundArgs[0] = Boolean.FALSE; 
  164.                 invokeMethod(mSetForeground, mSetForegroundArgs); 
  165.             } 
  166.         } 
  167.     } 
  168.  
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.Build.VERSION;
import android.util.Log;
public class ForegroundService extends Service {
private static final String TAG = "ForegroundService";
private boolean mReflectFlg = false;
private static final int NOTIFICATION_ID = 1; // 如果id设置为0,会导致不能设置为前台service
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];
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate");
mNM = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
try {
mStartForeground = ForegroundService.class.getMethod("startForeground", mStartForegroundSignature);
mStopForeground = ForegroundService.class.getMethod("stopForeground", mStopForegroundSignature);
} catch (NoSuchMethodException e) {
mStartForeground = mStopForeground = null;
}
try {
mSetForeground = getClass().getMethod("setForeground",
mSetForegroundSignature);
} catch (NoSuchMethodException e) {
throw new IllegalStateException(
"OS doesn't have Service.startForeground OR Service.setForeground!");
}
Notification.Builder builder = new Notification.Builder(this);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
builder.setContentIntent(contentIntent);
builder.setSmallIcon(R.drawable.icon);
builder.setTicker("Foreground Service Start");
builder.setContentTitle("Foreground Service");
builder.setContentText("Make this service run in the foreground.");
Notification notification = builder.build();
startForegroundCompat(NOTIFICATION_ID, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.d(TAG, "onStartCommand");
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
stopForegroundCompat(NOTIFICATION_ID);
}
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 (mReflectFlg) {
// 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);
} else {
/* 还可以使用以下方法,当sdk大于等于5时,调用sdk现有的方法startForeground设置前台运行,
* 否则调用反射取得的sdk level 5(对应Android 2.0)以下才有的旧方法setForeground设置前台运行 */
if(VERSION.SDK_INT >= 5) {
startForeground(id, notification);
} else {
// 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 (mReflectFlg) {
// 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);
} else {
/* 还可以使用以下方法,当sdk大于等于5时,调用sdk现有的方法stopForeground停止前台运行,
* 否则调用反射取得的sdk level 5(对应Android 2.0)以下才有的旧方法setForeground停止前台运行 */
if(VERSION.SDK_INT >= 5) {
stopForeground(true);
} else {
// 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);
}
}
}
}

前台Service运行后的效果如图:

(1).通知栏显示内容:


(2).下拉后通知栏显示内容:

 

最后

以上就是细心篮球为你收集整理的Android创建前台运行的Service的全部内容,希望文章能够帮你解决Android创建前台运行的Service所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(98)

评论列表共有 0 条评论

立即
投稿
返回
顶部