1

インターネットに接続されているときは常にアプリが何かをする必要があります。しかし、私はこれを行う必要がありますOUTSIDE私のアプリケーション。多くのアイデアがあり、ここでstackoverflowに役立ちますが、アプリケーションは常にWITHINです。私はAlarmManagerがアプリケーションの外で動作することを知っていますが、5分ごとにインターネットに接続されているかどうかを確認しようとすると、バッテリーが消耗してしまいます。私が望んだのは、アプリの外の接続をチェックして何かをダウンロードすることです。Androidインターネット接続チェッカーアプリの外側にある

インテントサービスもアプリケーション外でも動作することがわかりました。私はこれをWakeful Broadcast Receiverの中に入れようとしました。しかし、まだ呼び出されていません。

誰かが私を助けることができますか?ありがとう!

ここでこれは

パブリッククラスConnectivityOutsideAppServiceがIntentService {

private static final String LOG_TAG = ConnectivityOutsideAppService.class.getSimpleName(); 
private ConnectivityManager connectivityManager; 
private static boolean connection = false; 
private Context context; 

public ConnectivityOutsideAppService() { 
    super(ConnectivityOutsideAppService.class.getSimpleName()); 
    this.context = context; 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    connectivityManager = 
      (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    checkConnectionOnDemand(); 

    Log.e(LOG_TAG, " ## onHandleIntent##"); 
    if (connection == true 
      && intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, 
      false)) { 
     Log.e(LOG_TAG, " ## connection == true ##"); 
     connection = false; 
    } else if (connection == false 
      && !intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, 
      false)) { 
     Log.e(LOG_TAG, " ## connection == false ##"); 
     connection = true; 
    } 

    ConnectivityOutsideAppReceiver.completeWakefulIntent(intent); 
} 

public static boolean hasConnection() { 
    return connection; 
} 

private void checkConnectionOnDemand() { 
    Log.e(LOG_TAG, " ## checkConnectionOnDemand ##"); 
    final NetworkInfo info = connectivityManager.getActiveNetworkInfo(); 
    if (info == null || info.getState() != NetworkInfo.State.CONNECTED) { 
     if (connection == true) { 
      Log.e(LOG_TAG, " ## connection == true ##"); 
      connection = false; 
     } 
    } else { 
     if (connection == false) { 
      Log.e(LOG_TAG, " ## connection == false ##"); 
      connection = true; 
     } 
    } 
} 
} 

これは私のAndroidのマニフェスト

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

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

    <service 
     android:name="com.timetrackermobilelog.BusinessServices.ConnectivityOutsideAppService" 
     android:exported="false"/> 

    <receiver android:name="com.timetrackermobilelog.Utilities.ConnectivityOutsideAppService" 
       android:enabled="true" 
       android:process=":remote"> 

     <intent-filter android:priority="1000" > 
      <action android:name="com.timetrackermobilelog.Utilities.ConnectivityOutsideAppService" /> 
      <category android:name="com.Utilities" /> 
     </intent-filter> 
    </receiver> 
ですが拡張テントサービスである私の覚醒ブロードキャストレシーバー

public class ConnectivityOutsideAppReceiver extends WakefulBroadcastReceiver { 

    private static final String LOG_TAG = ConnectivityOutsideAppReceiver.class.getSimpleName(); 

    private ConnectivityManager connectivityManager; 
    private static boolean connection = false; 

    @Override 
    public void onReceive(Context context, Intent intent){ 

     ComponentName comp = new ComponentName(context.getPackageName(), 
       ConnectivityOutsideAppService.class.getName()); 

     // Start the service, keeping the device awake while it is 
     // launching. 
     startWakefulService(context, (intent.setComponent(comp) 

     )); 
    } 

} 

です

私はアクティビティ内でレシーバを登録しようとしましたが、うまく動作しません。

IntentFilter intentFilter = new IntentFilter("com.pointwest.timetrackermobilelog.Utilities.ConnectivityOutsideAppReceiver"); 
ConnectivityOutsideAppReceiver connectivityOutsideAppReceiver = new ConnectivityOutsideAppReceiver(); 
registerReceiver(connectivityOutsideAppReceiver, intentFilter); 

私が見逃したことはありますか?

+1

[ 'CONNECTIVITY_CHANGE'アクション](のマニフェストであなたのレシーバを登録http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html#MonitorChanges)を使用してください。 –

+0

あなたはあなたのコードをデバッグしています..?あなたはどこで問題を起こすべきですか? –

+0

こんにちは@MikeM。私はその1つをして、あなたに戻って取得します。 @VishalHalani。 –

答えて

2

@Mike M.のおかげで、ほんの数行の変更で答えが得られました。 Androidのマニフェストに

、それを変更します。

<intent-filter android:priority="1000" > 
       <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> 
      </intent-filter> 

とサービスのonHandleIntentは、それを変更します。

connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
関連する問題