J'ai une activité. Il recevra deux variables d'un service. Dans le service, je vais envoyer deux variables à l'activité parIncorrect lors de l'inscription de deux récepteurs dans une activité Android
// Send first variable
sendBroadcast(new Intent("first_one"));
// Send second variable
sendBroadcast(new Intent("second_one"));
Maintenant, dans l'activité, j'ai utilisé le code ci-dessous pour recevoir les données. Il y a
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");
}
};
Cependant, je ne peux pas imprimer le journal "OK seconde" quand j'ai appelé sendBroadcast (nouvelle intention ("second_one")); dans le service. Qu'est-ce qui se passe? Comment puis-je le réparer? Merci
MISE À JOUR: mon activité est une activité d'appel acceptée obtenir de @notz How can incoming calls be answered programmatically in Android 5.0 (Lollipop)?. Puis-je créer un service comme suit
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;
}
}
Quelle est l'erreur que vous obtenez? –
registerReceiver dans onResume et vérifiez. –
variable signifie la diffusion je suppose –