Service 在Android 6中,后台服务在一段时间后显示超时异常
这是一项在后台运行的服务,没有活动,开始正常运行,但大约四小时后,将出现Service 在Android 6中,后台服务在一段时间后显示超时异常,service,background,timeoutexception,Service,Background,Timeoutexception,这是一项在后台运行的服务,没有活动,开始正常运行,但大约四小时后,将出现ConnectTimeoutException 连接到xxx.xxx.xxx.xxx超时 这个问题发生在Android 6中,我没有发现Android 4中的这个问题。当这个问题发生时,我必须重新启动这个手机,然后它会正常连接一段时间。出现此问题时,手机上的其他网络应用程序会正常运行 public class mService extends Service{ Intent intent; private
ConnectTimeoutException
连接到xxx.xxx.xxx.xxx超时
这个问题发生在Android 6中,我没有发现Android 4中的这个问题。当这个问题发生时,我必须重新启动这个手机,然后它会正常连接一段时间。出现此问题时,手机上的其他网络应用程序会正常运行
public class mService extends Service{
Intent intent;
private Handler objHandlerCheckNetwork = new Handler();
private boolean mReflectFlg = false;
private static final int NOTIFICATION_ID = 101;
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];
private Runnable mHttpTestRunnable = new Runnable() {
@Override
public void run() {
if (httpTest()){
Log.e(GlobalData.getClassMethodName(),"true");
}else{
Log.e(GlobalData.getClassMethodName(),"false");
}
}
};
private Runnable mTasksCheckNetwork = new Runnable()
{
public void run()
{
Thread httpTestThread = new Thread(mHttpTestRunnable);;
httpTestThread.start();
objHandlerCheckNetwork.postDelayed(mTasksCheckNetwork, 1000*30);
}
};
@SuppressLint("NewApi")
@Override
public void onCreate() {
super.onCreate();
mNM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE );
try {
mStartForeground = mService.class.getMethod("startForeground" , mStartForegroundSignature);
mStopForeground = mService.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!");
}
Intent intent = new Intent(this,UploadTableDataService.class );
intent.putExtra( "ficationId", NOTIFICATION_ID);
Notification.Builder builder = new Notification.Builder(this);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentIntent(contentIntent);
builder.setSmallIcon(R.drawable.gps);
builder.setContentTitle( "test" );
builder.setContentText( "test111" );
Notification notification = builder.getNotification();
startForegroundCompat( NOTIFICATION_ID, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
//startService( new Intent( this, WifiService. class));
//startService( new Intent( this, VoiceService. class));
this.intent = intent;
Log.e(GlobalData.getClassMethodName(),"mService start!");
objHandlerCheckNetwork.postDelayed(mTasksCheckNetwork, 1000);
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
try{
objHandlerCheckNetwork.removeCallbacks(mTasksCheckNetwork);
}catch (Exception e) {
Log.d("DEBUG->", "onDestroy error - removeUpdates: ");
}
//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 {
if (Build.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;
}
mNM.cancel(id);
mSetForegroundArgs[0] = Boolean. FALSE;
invokeMethod( mSetForeground, mSetForegroundArgs);
} else {
if (Build.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);
}
}
}
public static Boolean httpTest() {
HttpClient client= new DefaultHttpClient();;
try {
StringBuilder sb = new StringBuilder();
HttpParams httpParams = client.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 1000*5);
HttpConnectionParams.setSoTimeout(httpParams, 1000*10);
HttpResponse response = client.execute(new HttpGet("http://www.itnanny.com/default.htm"));
HttpEntity entity = response.getEntity();
if (entity != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"), 8192);
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
reader.close();
}
Log.e(GlobalData.getClassMethodName(),"result:"+sb.toString());
if (sb.toString().indexOf("ok") > -1){
return true;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
client.getConnectionManager().shutdown();;
}
return false;
}
}
公共类mService扩展服务{
意图;
私有处理程序objHandlerCheckNetwork=新处理程序();
私有布尔值mReflectFlg=false;
私有静态最终整数通知_ID=101;
私有静态最终类[]mSetForegroundSignature=新类[]{boolean.Class};
私有静态最终类[]mStartForegroundSignature=新类[]{int.Class,Notification.Class};
私有静态最终类[]mStopForegroundSignature=新类[]{boolean.Class};
私人通知经理mNM;
私有方法mSetForeground;
私有方法;
私有方法;
私有对象[]mSetForegroundArgs=新对象[1];
私有对象[]mStartForegroundArgs=新对象[2];
私有对象[]mStopForegroundArgs=新对象[1];
private Runnable mhttpestrunnable=new Runnable(){
@凌驾
公开募捐{
if(httpTest()){
Log.e(GlobalData.getClassMethodName(),“true”);
}否则{
Log.e(GlobalData.getClassMethodName(),“false”);
}
}
};
专用可运行mTasksCheckNetwork=new Runnable()
{
公开募捐
{
线程httpTestThread=新线程(mHttpTestRunTable);;
httpTestThread.start();
objHandlerCheckNetwork.postDelayed(mTasksCheckNetwork,1000*30);
}
};
@SuppressLint(“新API”)
@凌驾
public void onCreate(){
super.onCreate();
mNM=(NotificationManager)getSystemService(Context.NOTIFICATION\u服务);
试一试{
mStartForeground=msservice.class.getMethod(“startForeground”,mStartForegroundSignature);
mStopForeground=msservice.class.getMethod(“stopForeground”,mStopForegroundSignature);
}捕获(无此方法例外){
mStartForeground=mStopForeground=null;
}
试一试{
mSetForeground=getClass().getMethod(“setForeground”,mSetForegroundSignature);
}捕获(无此方法例外){
抛出新的IllegalStateException(“操作系统没有Service.startForeground或Service.setForeground!”);
}
Intent Intent=新Intent(这是UploadTableDataService.class);
意向。额外(“证书ID”,通知ID);
Notification.Builder=new Notification.Builder(此);
PendingEvent contentIntent=PendingEvent.getActivity(this,0,intent,0);
builder.setContentIntent(contentIntent);
builder.setSmallIcon(R.drawable.gps);
builder.setContentTitle(“测试”);
builder.setContentText(“test111”);
Notification Notification=builder.getNotification();
startForegroundCompat(通知ID,通知);
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
super.onStartCommand(intent、flags、startId);
//startService(新意图(这个,WifiService.class));
//startService(新意图(这个,VoiceService.class));
本意=本意;
Log.e(GlobalData.getClassMethodName(),“mService start!”);
postDelayed(mTasksCheckNetwork,1000);
返回开始时间;
}
@凌驾
公共IBinder onBind(意向){
返回null;
}
@凌驾
公共空间{
super.ondestory();
试一试{
removeCallbacks(mTasksCheckNetwork);
}捕获(例外e){
d(“调试->”,“onDestroy错误-removeUpdates:”);
}
//stopForegroundCompat(通知ID);
}
void invokeMethod(方法,对象[]参数){
试一试{
调用(this,args);
}捕获(调用TargetException e){
//不应该发生。
Log.w(“ApiDemos”,“无法调用方法”,e);
}捕获(非法访问例外e){
//不应该发生。
Log.w(“ApiDemos”,“无法调用方法”,e);
}
}
/**
*这是一个新startForeground方法的包装器,使用旧的
*API(如果不可用)。
*/
void startForegroundCompat(int-id,通知){
如果(mReflectFlg){
//如果我们有新的startForeground API,那么就使用它。
if(mStartForeground!=null){
mStartForegroundArgs[0]=Integer.valueOf(id);
mStartForegroundArgs[1]=通知;
invokeMethod(mStartForeground、mStartForegroundArgs);
返回;
}
//回到旧的API上。
mSetForegroundArgs[0]=布尔值.TRUE;
调用方法(mSetForeground,mSetForegroundArgs);
mNM.通知(id,通知);
}否则{
如果(Build.VERSION.SDK_INT>=5){
startForeground(id,通知);
}否则{
//回到旧的API上。
mSetForegroundArgs[0]=布尔值.TRUE;
调用方法(mSetForeground,mSetForegroundArgs);
mNM.通知(id,通知);
}
}
}
/**
*这是一个围绕新stopForeground方法的包装器,使用旧的
*API(如果不可用)。
*/
void stopForegroundCompat(int-id){
如果(