2016-05-12 50 views
0

当我需要使用互联网连接并且它不可用时,我想设置一个广播接收器,在应用程序关闭时继续工作。保持连接广播接收器在应用程序关闭后运行

我正在使用该服务,但它不起作用。它可以在应用程序在屏幕上或暂停时正常工作,但如果关闭它,它将停止工作。我不确定我是否应该使用其他的东西,或者如果我做错了什么。

这是我的代码:

package com.example.ana.exampleapp; 

import android.app.Service; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.content.SharedPreferences; 
import android.os.IBinder; 
import android.util.Log; 

public class TestsService extends Service { 

    private static BroadcastReceiver networkChangeReceiver; 
    private static String TAG = "Service"; 

    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     Log.v(TAG, "Service created"); 
     boolean keep = needTokeepWaiting() 
     if(!keep) 
      stopSelf(); 
    } 

    @Override 
    public void onDestroy() { 
     if (networkChangeReceiver != null) 
      unregisterReceiver(networkChangeReceiver); 
     networkChangeReceiver = null; 
     Log.v(TAG, "Service destroyed"); 
    } 

    private void registerNetworkChangeReceiver() { 
     networkChangeReceiver = new BroadcastReceiver() { 
      private String TAG = "NetworkReceiver"; 

      @Override 
      public void onReceive(Context context, Intent intent) { 
       Log.v(TAG, "Connection changed received"); 
          boolean keep = needTokeepWaiting() 
       if(!keep) 
        stopSelf(); 
      } 
     }; 
     IntentFilter filter = new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION); 
     registerReceiver(networkChangeReceiver, filter); 
    } 
} 

而且我manisfest:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.ana.exampleapp"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".MainActivity" 
      android:windowSoftInputMode="stateHidden"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".RegisterActivity"></activity> 
     <activity android:name=".FinishRegisterActivity"></activity> 
     <activity android:name=".TestActivity"></activity> 
     <activity android:name=".InformationActivity"></activity> 
     <service android:name=".TestsService"></service> 
    </application> 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
</manifest> 

而且在测试活动时,有必要我做的:

Intent service = new Intent(this, TestsService.class); 
startService(service); 

什么我做的是类似于这里所建议的,但它不起作用:

Keep broadcast receiver running after application is closed

我也试图做没有一个服务,在清单中注册,但它没有工作也不:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.ana.exampleapp"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity" 
      android:windowSoftInputMode="stateHidden"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".RegisterActivity"> 
     </activity> 
     <activity android:name=".FinishRegisterActivity" > 
     </activity> 
     <activity android:name=".TestActivity"> 
     </activity> 
     <activity android:name=".InformationActivity" > 
     </activity> 
     <receiver android:name=".NetworkChangeReceiver" 
      android:enabled="false"> 
      <intent-filter> 
       <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> 
      </intent-filter> 
     </receiver> 
    </application> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
</manifest> 
+0

您不需要为该广播持续运行“服务”。您可以在清单中为''android.net.conn.CONNECTIVITY_CHANGE''''注册一个''。 –

+0

我曾试过,但当我关闭应用程序停止工作。我已经添加了如何在清单中注册,也许我做错了什么。 – ana06

+0

我看到你想出你的'服务'问题,但你真的不需要它一直运行。你的''被禁用。将'enabled'属性的值更改为'true',或者将其删除,并确保'name'指向一个有效的'BroadcastReceiver'类。您需要在安装后至少启动一次您的应用程序,以使其脱离_stopped_状态,但在此之后,只要连接发生变化,您的Receiver就会触发。 –

回答

0

我刚刚发现了这个问题。 Android 4.4.x中存在一个错误,它会在关闭应用程序时终止后台服务。我正在Android 4.4.2中测试我的应用程序。这里有一个详细的解释:

http://www.androidpolice.com/2014/03/07/bug-watch-stopping-apps-on-android-4-4-2-can-silently-kill-related-background-services-a-fix-is-on-the-way/

所以我的代码是正确的,因为我在安卓4.2.2试图和它的作品(我曾尝试重写onStartCommand()所以也许认为需要)。

-1

如果你不想让你的服务停止时应用程序被关闭,你应该重写onStartCommand()方法做这样的事情:

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    return START_REDELIVER_INTENT; 
} 

您可以返回START_STICKYSTART_REDELIVER_INTENT,任何可以更好

+0

它不起作用。我想这可能是服务不会停止,当我关闭应用程序,只是BroadcastReceiver停止工作,虽然我不知道。我的意思是故意关闭应用程序。用户在“最近的应用程序”屏幕上刷出应用程序后。 – ana06

+1

你应该确定服务是否停止。尝试使用断点或查看logcat来检查是否调用了'onDestroy()' –

+0

'onDestroy()'未被调用。 – ana06

相关问题