2017-12-19 13 views
0

SyncAdapterのアプリケーションが正常に動作しています。 SyncServiceは1時間に1回実行されます。SyncAdapterからアクティビティにsendBroadcastを送信できません

私のアプリケーションには、FragmentActivityというDeviceControlActivityと呼ばれるメインがあり、syncResultなどのSyncAdaptorからメッセージを受け取りたいと考えています。

私はそれを稼働させるために多くの試みをしましたが、DeviceControlActivityのメッセージはいずれもSyncAdapterから届きません。私は私のDeviceControlActivity

:SyncAdapterオン

private BroadcastReceiver mSyncMessageReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d(TAG, "Got it!"); 
    } 
}; 

@Override 
protected void onResume() { 
    super.onResume(); 
    // Receive feedback from syncManager 
    LocalBroadcastManager.getInstance(getApplication()).registerReceiver(mSyncMessageReceiver, 
      new IntentFilter(Constants.MESSAGE_SYNC)); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mSyncMessageReceiver); 
} 

を、私が持っている:

private LocalBroadcastManager mBroadcastManager; 

public SyncAdapter(Context context, boolean autoInitialize) { 
    super(context, autoInitialize); 


    mBroadcastManager = LocalBroadcastManager.getInstance(context); 
} 

@Override 
public void onPerformSync(
     Account account, 
     Bundle extras, 
     String authority, 
     ContentProviderClient provider, 
     SyncResult syncResult) { 

    Intent intent = new Intent(Constants.MESSAGE_SYNC); 
    mBroadcastManager.sendBroadcast(intent); 

私はDeviceControlActivityから自分自身にメッセージを送信することができ、それが動作します。

私はSyncServiceからメッセージを受信しますが、別の方法を使用してすることができます:予想通り

final int mask = ContentResolver.SYNC_OBSERVER_TYPE_PENDING | ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE; 
mSyncMonitor = ContentResolver.addStatusChangeListener(mask, this); 

このリスナーは同期の活動を報告します。

しかし、LocalBroadcastManagerを使用して、サービスとアクティビティの間で通信したいと思いますが、できません。

私はすでにLocalBroadcastManager文書によると、このクラスはに登録して、あなたのプロセス内ローカルオブジェクトにインテントのブロードキャストを送信するためにヘルパーですHow to use LocalBroadcastManager?

答えて

1

のような関連の質問をチェックしてきました。

<service>が別のプロセスで実行されている場合、上記の例は機能しません。

AndroidManifest.xmlされました:

<service 
      android:name=".sync.SyncService" 
      android:exported="true" 
      android:process=":sync"> 

これはCreating a Sync Adapterで提案方法です。

我々は<service>宣言を変更した場合:android:processプロパティを削除

<service 
     android:name=".sync.SyncService" 
     android:exported="true"> 

SyncServiceは同じプロセスで実行され、LocalBroadcastManagerが動作します。

関連する問題