在Service和BroadcastReceiver之间发送额外消息

时间:2013-11-21 16:56:49

标签: android data-binding service broadcastreceiver

在我的应用程序中,我有TextField和按钮。单击时,Service将启动并从TextField读取值并触发AlarmManager。几秒钟后,服务开始BroadcastReceiverBrodcastReceiver开始第二个Activity

Activity通知服务并发送值。我可以在Toast中看到Service的号码正确无误。但是在第二次Activity中,我只看到了第一次输入的值。

例如:我写了5并且它计数到5并且启动​​BroadcatsReceiver并且在Activity中我可以看到5.但是如果我现在放3,它将计为3,但是在secound活动中显示5(示例在图片如下)。下一步:我将8的数量计算为8,首先正确显示Toast。在Secound活动中仍然:5。即使我再次上传手机程序。只有查看正确值的方法是删除应用程序并再次启动它。我该如何解决?

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);  
    }

我的活动:

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

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

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

    }

Example. LogCat

1 个答案:

答案 0 :(得分:0)

好的,假设您希望从活动A转移到B

1:在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:读取TextView值:

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

3:将其解析为数字,例如

int timeToWait = Integer.parseInt(message);

4:等待使用延迟处理程序并启动活动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:在活动B中通过覆盖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);
}