Java Android BroadcastReceiver onReceive 更新 MainActivity 中的 TextView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22869928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android BroadcastReceiver onReceive Update TextView in MainActivity
提问by user3499199
In MainActivity I have a TextView: textV1. I also have a method in MainActivity that updates that textview:
在 MainActivity 我有一个 TextView:textV1。我在 MainActivity 中还有一个方法可以更新该文本视图:
public void updateTheTextView(final String t) {
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
TextView textV1 = (TextView) findViewById(R.id.textV1);
textV1.setText(t);
}
});
}
In a BroadcasrReceiver I need to update the text in textV1 in MainActivity.
在 BroadcasrReceiver 中,我需要更新 MainActivity 中 textV1 中的文本。
public class NotifAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// other things done here like notification
// NEED TO UPDATE TEXTV1 IN MAINACTIVITY HERE
}
}
How can this be done? The BroadcastReceiver is run from a service. This code I cannot change. Can I access and change textV1 in MainActivity from onReceive()? I've tried many things but all fail.
如何才能做到这一点?BroadcastReceiver 从服务运行。此代码我无法更改。我可以从 onReceive() 访问和更改 MainActivity 中的 textV1 吗?我尝试了很多东西,但都失败了。
采纳答案by User10001
In your MainActivity
initialize a variable of MainActivity
class like below.
在你MainActivity
初始化一个MainActivity
类的变量,如下所示。
public class MainActivity extends Activity {
private static MainActivity ins;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ins = this;
}
public static MainActivity getInstace(){
return ins;
}
public void updateTheTextView(final String t) {
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
TextView textV1 = (TextView) findViewById(R.id.textV1);
textV1.setText(t);
}
});
}
}
public class NotifAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
MainActivity .getInstace().updateTheTextView("String");
} catch (Exception e) {
}
}
}
回答by ShahinFasihi
create an instance of the class and then pass the value to the function that changes TextView value follow these steps please : in your BroadcastReceiver overRide onReceive method and paste These lines or changes theme as you wish
创建该类的实例,然后将值传递给更改 TextView 值的函数,请按照以下步骤操作:在您的 BroadcastReceiver 中覆盖 onReceive 方法并根据需要粘贴这些行或更改主题
private Handler handler = new Handler(); // Handler used to execute code on the UI thread
// Post the UI updating code to our Handler
handler.post(new Runnable() {
@Override
public void run() {
//Toast.makeText(context, "Toast from broadcast receiver", Toast.LENGTH_SHORT).show();
YourActivityToUpdate.updateTheTextView(message);
YourActivityToUpdateinst = YourActivityToUpdate.instance();
if(inst != null) { // your activity can be seen, and you can update it's context
inst.updateTheTextView(message);
}
}
});
now we explain the updateTheTextView and inst in YourActivityToUpdate class Paste these Lines please
现在我们解释一下 YourActivityToUpdate 类中的 updateTheTextView 和 inst 请粘贴这些行
private static SignUpVerify mInst;
public static SignUpVerify instance() {
return mInst;
}
@Override
public void onStart() {
super.onStart();
mInst = this;
}
@Override
public void onStop() {
super.onStop();
mInst = null;
}
and this is the updateTheTextView method that should be placed in YourActivityToUpdate class
这是应该放在 YourActivityToUpdate 类中的 updateTheTextView 方法
public void updateTheTextView(final String verifyCodeValue) {
Log.i("verifyCodeValue", verifyCodeValue);
YourTextViewToUpdate.setText(verifyCodeValue);
}
i think this is a better way thanks to "kevin-lynx"
由于“kevin-lynx”,我认为这是一个更好的方法
回答by Renato Aloi
If someone is searching this exact solution, but in Kotlin, do the following:
如果有人正在搜索这个确切的解决方案,但在 Kotlin 中,请执行以下操作:
class MainActivity : AppCompatActivity() {
companion object {
var ins: MainActivity? = null
fun getInstance(): MainActivity? {
return ins
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
ins = this
}
fun updateTheTextView(t: String) {
[email protected] {
val textV1 = findViewById<TextView>(R.id.textV1)
textV1.text = t
}
}
}
class NotifAlarm : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
try {
MainActivity.getInstance()?.updateTheTextView("The String")
} catch (e: Exception) {
}
}
}
回答by Rohit Singh
Use Interface
使用界面
Another way to deal with this situation is by using an Interface. I will describe the advantage of using this approach but first lets see how its done.
处理这种情况的另一种方法是使用接口。我将描述使用这种方法的优势,但首先让我们看看它是如何完成的。
Follow these steps:
1) Create an interface
请按照以下步骤操作:
1) 创建接口
public interface MyBroadcastListener{
public void doSomething(String result);
}
2) Initialize the listener in BroadCastReceiver
2) 在 BroadCastReceiver 中初始化监听器
public class NotifAlarm extends BroadcastReceiver {
private MyBroadcastListener listener;
@Override
public void onReceive(Context context, Intent intent) {
listener = (MyBroadcastListener)context;
// other things done here like notification
// NUPDATE TEXTV1 IN MAINACTIVITY HERE
listener.doSomething("Some Result");
}
}
3) Implement the interface in Activity and override the method
3)在Activity中实现接口并覆盖方法
public YourActivity extends AppCompatActivity implements MyBroadcastListener{
// Your Activity code
public void updateTheTextView(String t) {
TextView textV1 = (TextView) findViewById(R.id.textV1);
textV1.setText(t);
}
@Override
public void doSomething(String result){
updateTheTextView(result); // Calling method from Interface
}
}
Advantage of using interface?
使用接口的优势?
- When you have BroadcastReceiver in a different file
- Decoupled BroadcastReceiver
- 当您在不同的文件中有 BroadcastReceiver 时
- 解耦广播接收器
Using interface approach makes BroadcastReceiver independent of any Activity. Lets say in future you want to use this BroadCastReceiver with Another Activity which takes the result from BroadcastReceiver and start a DetailActivity with the result. This is completely different Task but You will use Same BroadcastReceiver without even changing the code inside BroadcastReceiver.
使用接口方法使 BroadcastReceiver 独立于任何 Activity。假设将来您希望将此 BroadCastReceiver 与另一个活动一起使用,该活动从 BroadcastReceiver 获取结果并使用结果启动 DetailActivity。这是完全不同的任务,但您将使用 Same BroadcastReceiver,甚至无需更改 BroadcastReceiver 中的代码。
How to do that?
Implement the interface in the Activity and Override the method. That's it!
怎么做?
在Activity中实现接口并覆盖方法。就是这样!
public ListActivity extends AppCompatActivity implements MyBroadcastListener{
// Your Activity code
public void startDetailActivity(String title) {
Intent i = new Intent(ListActivity,this, DetailActivity.class);
i.putExtra("Title", title);
startActivity(i);
}
@Override
public void doSomething(String result){
startDetailActivity(String title); // Calling method from Interface
}
}
回答by Jimale Abdi
In your broadcastreceiver class send broadcast
在您的广播接收器类中发送广播
public class mybroadcaster extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
context.sendBroadcast(new Intent("updatetext"));
}
}
In your activity register your broadcastreceiver and call it, do your work at onReceive
and unregister the broadcaster in onDestroy()
在您的活动中注册您的广播接收器并调用它,onReceive
在onDestroy()
public class MyActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver(broadcastReceiver, new IntentFilter("updatetext"));
}
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// do your work here
}
};
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(broadcastReceiver);
}
}