将Intent从BroadcastReceiver类发送到当前正在运行的活动 [英] Sending Intent from BroadcastReceiver class to currently running activity

查看:96
本文介绍了将Intent从BroadcastReceiver类发送到当前正在运行的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展 BroadcastReceiver 的类。收到短信后,我想将信息传递到我的主要活动班级中,以在框中显示文本(附加,如果已经存在文本,则显示文本)。

I have a class which extends BroadcastReceiver. On receiving a SMS, I would like to pass information to my main activity class to display the text in a box (Append, if already text is present).

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

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

}

我只有一个活动班级( MainActivity.java )。但是,当我这样做时,我得到一个异常,无法暂停活动。

I have only one activity class (MainActivity.java). However When i do this I get an exception Unable to pause Activity.

推荐答案

您有以下三种方式:

1)您可以在 MainActivity 中定义广播,如下所示:

onCreate()

You have three ways:
1) You can define your broadcast inside your MainActivity like this:
in onCreate()

registerReceiver(smsReceiver, new IntentFilter(SMS_RECIEVED));  

并在 MainActivity

and define smsReciver in MainActivity

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

定义一个可运行的界面来更新UI

define a runnable to update UI

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

和更新方法

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

2)在您的Activity和BroadCastReceiver之间注册接收器

2) Register a receiver between your Activity and BroadCastReceiver

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

3) Start your Activity with new Intent to update current open 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

UPDATE :
explain method 2
MainActivity.class

onResume()

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

onDestroy()

unregisterReceiver(broadcastReceiver);

本地broadCast(broadcastReceiver,在MainActivity.class中)

local broadCast (broadcastReceiver, in 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

全局属性

SmsReceiver.class
global attribute

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

这篇关于将Intent从BroadcastReceiver类发送到当前正在运行的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆