将 Intent 从 BroadcastReceiver class 发送到当前 运行 activity

Sending Intent from BroadcastReceiver class to currently running activity

我有一个扩展 BroadcastReceiver 的 class。收到短信后,我想将信息传递给我的主 activity class 以在框中显示文本(附加,如果已经存在文本)。

public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
    {
        Intent i = new Intent(context, MainActivity.class);
            i.putExtra("updatedString","Hello");
            context.startActivity(i);
    }
}

MainActivity.java

public class MainActivity extends Activity{

  private TextView results;
  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Bundle extras = getIntent().getExtras();
        if(extras!=null){
            results = (TextView) findViewById(R.id.results);
            results.setVisibility(View.VISIBLE);
            results.append(extras.getString("updatedString"));
        }

}

我只有一个activity class (MainActivity.java)。然而,当我这样做时,我得到一个异常 Unable to pause Activity.

你有三种方法:
1) 您可以像这样在 MainActivity 中定义您的广播:
onCreate()

registerReceiver(smsReceiver, new IntentFilter(SMS_RECIEVED));  

并在 MainActivity

中定义 smsReciver
private BroadcastReceiver smsReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        //you can update textBox here
        handler.postDelayed(sendUpdatesToUI, 10);  
    }
};  

定义一个可运行的更新UI

private Runnable sendUpdatesToUI = new Runnable() {
    public void run() {
        update();
    }
};

和更新方法

private void update(String text) {
    textView.setText(textView.getText().toString() + text);
} 

2) 在您的 Activity 和 BroadCastReceiver

之间注册一个接收器

3) 使用新 Intent 启动您的 Activity 以更新当前打开的 Activity

Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("Key", "text");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);  

更新:
解释方法2
MainActivity.class

onResume()

registerReceiver(broadcastReceiver, new IntentFilter(SmsReceiver.BROADCAST_ACTION));  

onDestroy()

unregisterReceiver(broadcastReceiver);

本地广播(broadcastReceiver,在MainActivity.class)

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        updateUI(intent);
    }
};
private void updateUI(Intent intent) {
    String text = intent.getStringExtra("key");
    textView.setText(textView.getText().toString() + text);
}

SmsReceiver.class
全局属性

public static final String BROADCAST_ACTION = "your.package.name.displayevent";
private final Handler handler = new Handler();
Intent intent;
Context context;

onReceive()

handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 10);

this.context = context;//you can retrieve context from onReceive argument

this.intent = new Intent(BROADCAST_ACTION);

定义两个方法

private Runnable sendUpdatesToUI = new Runnable() {
    public void run() {
        display();
    }
};

private void display() {
    intent.putExtra("key", text);
    context.sendBroadcast(intent);
}

修改您的代码如下。

 public class SmsReceiver extends BroadcastReceiver {
@Override 

public void onReceive(Context context, Intent intent)
    { 
        Intent i = new Intent(context, MainActivity.class);
            i.putExtra("updatedString","Hello");            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
            context.startActivity(i);
    } 
}

public class MainActivity extends Activity{

  private TextView results;
  @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Bundle extras = getIntent().getExtras();
        if(extras!=null){
            results = (TextView) findViewById(R.id.results);
            results.setVisibility(View.VISIBLE);
            results.append(extras.getString("updatedString"));
        } 

@Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
      //handle your intent here.Note this will be called even when activity first created.so becareful to handle intents correctly.
    }

}