简体   繁体   中英

Sending Extra Message between Service and BroadcastReceiver

In my app I have TextField and button. When clicked, Service starts and reads value from TextField and triggers AlarmManager . After few seconds Service starts BroadcastReceiver and BrodcastReceiver start Second Activity .

Activity informs Service and sends value. I can see Toast with correct number in Service . But in Second Activity I see only the value that was entered for the first time.

For example: I wrote 5 and it's count to 5 and start BroadcatsReceiver and in Activity I can see 5. But if I will put right now 3, it will count to 3, but in secound activity show 5(example on the picture below). Next: I puts 8 it's count to eight, show correctly first Toast. In Secound activity still: 5. Even if I will upload program on phone one more time. Only way to see correct value is to delete application and starts it one more time. How can I fix it?

Method in MainActivity:

public void ClickStart(View view) {
        Intent i = new Intent(this, MyService.class);
        EditText numerField = (EditText) findViewById(R.id.numerField);
        String message = numerField.getText().toString();
        i.putExtra("EXTRA_MESSAGE", message);
        this.startService(i);
    }

MyService:

public class MyService extends Service {
    @Override
    public void onStart(Intent intent, int startId) {

        int countTime = 10;
        String data = (String) intent.getExtras().get("EXTRA_MESSAGE");
       countTime = Integer.parseInt(data);

        Intent i = new Intent(this, MyReciver.class);
        i.putExtra("EXTRA_MESSAGE", data);
        Log.d("dddService", data);  // Correct data

        Toast tosty = Toast.makeText(this, data + " sec", Toast.LENGTH_SHORT);
        tosty.show();           

        PendingIntent pintent = PendingIntent.getBroadcast(
                this.getApplicationContext(), 1, i, 0);

        AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                + (countTime * 1000), pintent);

    }

MyReceiver:

public void onReceive(Context arg0, Intent arg1) {


        String data = (String) arg1.getExtras().get("EXTRA_MESSAGE");
        Log.d("ddd", data);  //wrrong data
        Intent i = new Intent(arg0, MyActivity.class);
        i.putExtras(arg1.getExtras());
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        arg0.startActivity(i);  
    }

My activity:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String message = getIntent().getStringExtra("EXTRA_MESSAGE");

        Toast.makeText(getApplicationContext(), message + " Activity", Toast.LENGTH_SHORT)
                .show();

    }

例。 LogCat

Ok, assuming you wish to move from activity A to B

1: in A register a BroadcastReceiver

// declare a field variable for the BroadcastReceiver
private BroadcastReceiver startActivityB;
// inside onCreate
startActivityB = new StartActivityB();
registerReceiver(startActivityB, new IntentFilter("INTENT_WAKEUP_B");

// inside onDestroy
unregisterReceiver(startActivityB);

// implementation of startActivityB as a private class inside A
private class StartActivityB extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {

        String data = (String) arg1.getStringExtra("EXTRA_MESSAGE");
        Log.d("ddd", data); 
        Intent i = new Intent(arg0, MyActivity.class);
        i.putExtras(arg1.getExtras());
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        startActivity(i);  
    }
}

2: read the TextView value as you already do:

final String message = intent.getStringExtra("EXTRA_MESSAGE");

3: parse it to a numer, for example

int timeToWait = Integer.parseInt(message);

4: wait using a delayed handler and start activity B

    new Handler().postDelayed(new Runnable() {

        public void run() {
            Intent intent = new Intent("INTENT_WAKEUP_B");
                            intent.putExtra("EXTRA_MESSAGE",message);
                            sendBroadcast(intent);
        }
    }, timeToWait * 1000); // * 1000 if timeToWait is in seconds 

5: in activity B fetch the message by overriding onNewIntent

@Override
protected void onNewIntent(Intent intent) {
    String message = intent.getStringExtra("EXTRA_MESSAGE");


    Toast.makeText(getApplicationContext(), message + " Activity", Toast.LENGTH_SHORT)
            .show();
    super.onNewIntent(intent);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM