2016-12-26 5 views
2

활동이 있습니다. 서비스에서 두 개의 변수를받습니다. 이 서비스에서 나는 두 변수를 액티비티로 보낼 것이다.액티비티에 수신기 두 개를 등록 할 때 잘못된 경우

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

이제 활동에서 벨로우즈 코드를 사용하여 데이터를 수신했다. 나는() "second_one"새로운 의도() sendBroadcast를 호출 할 때 나는 "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"); 
     } 
    }; 

그러나이있다; 서비스에서. 무슨 일이야? 어떻게 해결할 수 있습니까? 고마워요

업데이트 : 내 활동은 수락 외재 활동입니다 @notz에서 가져 오기 How can incoming calls be answered programmatically in Android 5.0 (Lollipop)?.

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

어떤 오류가 발생합니까? onResume에있는 –

+0

registerReceiver를 확인하십시오. –

+0

변수는 브로드 캐스트를 의미합니다. –

답변

0

당신은 그에게 방법의 반대에 reсeiver 등록을 취소해야 다음과 같은 그럼 내가하는 당신이 그것을 등록 서비스를 만들 : 경우에서 onCreate()에서 등록 된 수신기 - 다음들의 OnDestroy에서 등록을 취소한다() . 하지만 대부분의 경우 가장 좋은 방법은 onResume()에 수신기를 등록하고 onPause()에서 등록을 취소하는 것입니다.