从Android中的BroadcastReceiver调用Activity方法

Jay*_*yas 29 methods android static-methods broadcastreceiver android-activity

在这里,我正在创建一个仅依赖于Internet的在线应用程序.

因此,每当出现网络错误时,都必须通知用户.为此,我创建了一个BroadcastReciver,它在网络连接丢失时接收呼叫(Internet).

这一切都很完美.现在我需要的是我必须从这个广播接收器调用一个Activity方法,在那里我创建了一个Alert Dialogue.

我在stack-overflow.com上读了很多答案,我可以声明该方法是静态的,只使用Activity名称调用,

例如 MyActivityName.myMethod()

但我不能声明我的方法是静态的,因为我在那里使用Alert Dialogue,它显示我在线错误,

AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
Run Code Online (Sandbox Code Playgroud)

不能在静态的环境中使用此.

那么,如何从广播接收器调用Activity方法(必须不是静态且不启动该活动)?

我可以从当前正在运行的广播接收器获取活动(或片段)名称吗?

Vij*_*jju 68

试试这段代码:

互联网丢失类的广播接收器类:

public class InternetLostReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
    context.sendBroadcast(new Intent("INTERNET_LOST"));
}
}
Run Code Online (Sandbox Code Playgroud)

在你的活动中添加这个用于呼叫广播:

public class TestActivity  extends Activity{

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

    registerReceiver(broadcastReceiver, new IntentFilter("INTERNET_LOST"));
}

BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // internet lost alert dialog method call from here...
    }
};

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(broadcastReceiver);
}
}
Run Code Online (Sandbox Code Playgroud)

  • 旁注:内部“BroadcastReceiver”不得与外部“BroadcastReceiver”具有相同的意图过滤器。如果他们这样做,你将陷入无限循环。 (2认同)

Roh*_*ngh 10

接口:将 BroadCastReceiver 和 Activity 代码分开!

您可以创建一个 CallBackListener 接口。该接口将作为BroadcastReceiver和之间的桥梁Activity

1)创建一个回调监听器

interface ConnectionLostCallback{

      public void connectionLost();

} 
Run Code Online (Sandbox Code Playgroud)

2)ConnectionLostCallback在您的 BroadcastReceiver 中提供

public class MyBroadcastReceiver extends BroadcastReceiver{

     private ConnectionLostCallback listener;

     public MyBroadcastReceiver(ConnectionLostCallback listener ){

           this.listener = listener     //<-- Initialze it

     }

     @Override
     public void onReceive(Context context, Intent intent) {

           listener.connectionLost();

     }
}
Run Code Online (Sandbox Code Playgroud)

3)ConnectionLostCallback在您的活动中实现并覆盖该方法

YourActvity extends AppcompatActivity implements ConnectionLostCallback{

    // Your Activity related code //
      //    new MyBroadcastReceiver(this);  <-- create instance

    private void showAlertMessage(){
       AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
    } 


    @Override 
    public void connectionLost(){

         showAlertMessage();          //<--- Call the method to shoe alert dialog

    }


}
Run Code Online (Sandbox Code Playgroud)

相关链接:

如果您想知道如何使 BroadcastReceiver 独立于任何活动,即如何将相同的 BroadCastReceiver 与不同的活动重用?然后阅读这个

  • 如果无法调用构造函数怎么办?例如,使用 AlarmManager 时,从 Alarmmanager 方法调用广播接收器 (2认同)