简体   繁体   English

来自BroadcastReceiver的MainActivity中的调用方法 - Android

[英]Call method in MainActivity from BroadcastReceiver - Android

I want to call method in Main Activity from Broadcast Receiver. 我想从广播接收器调用主要活动中的方法。 This is my MainActivity 这是我的MainActivity

  public class MainActivity extends ActionBarActivity {

        protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_main);
        }

  public void DisplayConn(){
       if(isNetworkStatusAvailable(getApplicationContext())) {

           Toast.makeText(getApplicationContext(), "internet is available", Toast.LENGTH_LONG).show();
       } else {
           AlertDialog.Builder builder = new AlertDialog.Builder(this);
           builder.setTitle("Error");
           builder.setMessage("No Network Connection").setCancelable(false)

                   .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           finish();
                       }
                   });
           AlertDialog alert = builder.create();
           alert.show();
       }
   }
}

And this is my BroadcastReceiver 这是我的BroadcastReceiver

 public class ConnectionReceiver extends BroadcastReceiver {

     public ConnectionReceiver() {
     }

     @Override
     public void onReceive(Context context, Intent intent) {
         MainActivity myAct = new MainActivity();
         myAct.DisplayConn();
     }
 }

So, everytime my Broadcast have receive it will call the methods in my MainActivity. 因此,每次我的广播都收到它,它将调用我的MainActivity中的方法。 Thanks in Advance. 提前致谢。

You can use LocalBroadcast Manager for sending a local broadcast from your ConnectionReceiver . 您可以使用LocalBroadcast ManagerConnectionReceiver发送本地广播。 In the MainActivity you can register your receiver to receive local broadcasts. MainActivity您可以注册接收器以接收本地广播。 You can send a local broadcast when onReceive is called, which will be received by your Activity. 您可以在调用onReceive时发送本地广播,您的活动将收到该广播。 Then in your activity you can call the method when you receive this local broadcast. 然后在您的活动中,您可以在收到本地广播时调用该方法。 This broadcast is only local to your app. 此广播仅适用于您的应用。 So is safe as well. 所以也是安全的。 You can see here how to use it: how to use LocalBroadcastManager? 你可以在这里看到如何使用它: 如何使用LocalBroadcastManager? .

LocalBroadcastManager is a helper to register for and send broadcasts of Intents to local objects within your process. LocalBroadcastManager是一个帮助程序,用于注册和发送Intent广播到进程中的本地对象。 This is has a number of advantages over sending global broadcasts with sendBroadcast(Intent). 与使用sendBroadcast(Intent)发送全局广播相比,这有许多优点。 One of them is that the data you are broadcasting won't leave your app, so don't need to worry about leaking private data. 其中之一是您播放的数据不会离开您的应用,因此不必担心泄露私人数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 AlarmManager BroadcastReceiver 调用 MainActivity 方法? - How to call MainActivity method from AlarmManager BroadcastReceiver? 有没有办法从BroadActivastReceiver的onReceive内部的MainActivity调用方法? - Is there a way to call a method from MainActivity inside BroadCastReceiver onReceive? 从android中的另一个类调用MainActivity方法 - Call MainActivity method from another class in android 在Android的“ MainActivity”中调用方法 - Call a method from my “MainActivity” in Android 来自外部广播接收器的MainActivity中的访问方法和变量 - Access method and varaible in MainActivity from extern BroadcastReceiver Android MainActivity和BroadcastReceiver - Android MainActivity and BroadcastReceiver 无法在 Android 中调用 MainActivity 的方法 - Cannot call method of MainActivity in Android 如何从扩展的BroadcastReceiver类调用MainActivity函数 - How to call MainActivity function from the extended BroadcastReceiver class Android:MainActivity中的方法运行良好,通过BroadcastReceiver类在MainActivity中运行相同的方法会给出空指针 - Android: Method in MainActivity works fine, running same method in MainActivity via BroadcastReceiver class gives null pointer 如何在Android中调用与MainActivity不同的活动的方法? - How to call a method of a different activity from MainActivity in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM