0

を使用して、サービスからアクティビティにブロードキャストを送信しようとしましたIntentService。そのために、次のコードを使用しました。

public class NotifyService extends IntentService {

    public NotifyService() {
        super("NotifyService");
    }

    // will be called asynchronously by Android
    @Override
    protected void onHandleIntent(Intent intent) {

        Log.d("onHandleIntent", "start service");
        publishResults();
    }

    private void publishResults() {

        result = Activity.RESULT_OK;
        Intent intent = new Intent(NOTIFICATION);
        intent.putExtra(RESULT, result);
        sendBroadcast(intent);

    }
}

次に、Activity クラスで Receiver を次のように定義します。

public BroadcastReceiver receiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("TAG", "receiver");
        Bundle bundle = intent.getExtras();


        if (bundle != null) {
            int resultCode = bundle.getInt(NotifyService.RESULT);
            if (resultCode == RESULT_OK) {

                Toast.makeText(Home.this, "after service work.", Toast.LENGTH_LONG)
                        .show();                    
            }
        }

         stopService(new Intent(Home.this,NotifyService.class));
    }
};

in メソッドとinメソッドを使用registerReceiverしましたonResumeunregisterReceiveronPause

registerReceiver(receiver, new IntentFilter(NotifyService.NOTIFICATION));

しかし、onReceiveメソッドが呼び出されない、

このサイトのセクション 7 を使用しました

私は何を逃したのですか?

編集

別の解決策はありましたか?サービスからアクティビティを通知してデータを更新しようとしました。

4

1 に答える 1