2016-12-26 12 views
2

を2つの受信を登録するとき、私は活性を有しています。サービスから2つの変数を受け取ります。サービスでは、私が今活動にAndroidの

// Send first variable 
sendBroadcast(new Intent("first_one")); 
// Send second variable 
sendBroadcast(new Intent("second_one")); 

によって活動への2つの変数を送信します、アクティビティで、私は、データを受信するには以下のコードを使用していました。私はsendBroadcast(新しいテント(「second_oneを」))と呼ばれるしかし、私は「第二のOK」ログを印刷することはできません

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     registerReceiver(firstRec, new IntentFilter("first_one")); 
     registerReceiver(secondRec, new IntentFilter("second_one")); 
    } 
    @Override 
    protected void onPause() { 
     super.onPause(); 
     if (firstRec != null) { 
      unregisterReceiver(firstRec); 
      firstRec = null; 
     } 
     if (secondRec != null) { 
      unregisterReceiver(secondRec); 
      secondRec = null; 
     } 
    } 
    private BroadcastReceiver firstRec = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      Log.d("TAG","OK first"); 
     } 
    }; 

    private BroadcastReceiver secondRec = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      Log.d("TAG","OK second"); 
     } 
    }; 

があります。サービスで。何が起こるのですか?どうすれば修正できますか?ありがとうございます

更新:私の活動は受け入れ活動ですgetn @notzから How can incoming calls be answered programmatically in Android 5.0 (Lollipop)?。そして、私はあなたがそれを登録すると、メソッドは逆に、あなたのreсeiverの登録を解除する必要があり

public class myService extends Service{ 
@Override 
    public void onCreate() { 
     super.onCreate(); 
} 
@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 
@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Intent answerCalintent = new Intent(getApplicationContext(), AcceptCallActivity.class); 
    answerCalintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 
    answerCalintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(answerCalintent); 
    //Send the second command after 10 second and make the calling in background 
    new CountDownTimer(10000, 100) { 
     public void onTick(long millisUntilFinished) { 
     } 
     public void onFinish() { 
      sendBroadcast(new Intent("second_one")); 
     } 
    }.start(); 

    return START_STICKY; 
} 
} 
+0

あなたが得るエラーは何ですか? –

+0

onResumeのregisterReceiverをチェックしてください。放送 –

+0

変数の手段は、私は「それが私をHELOする方法」を取得していない、あなたが放送 –

答えて

0

を次のようなサービスを作成する:あなたはのonCreateにおける登録レシーバ()場合 - そして、あなたは(onDestroyでそれを登録解除する必要があります) 。 しかし、私が知っているように、ほとんどの場合のために、ベストプラクティスは)(onResumeに受信機を登録)し、onPause(でそれを登録解除することです。

関連する問題