简体   繁体   English

如何在 BroadcastReceiver 中访问 MainActivity 的对象或方法?

[英]How can I access MainActivity's objects or methods inside BroadcastReceiver?

I'm creating an android app that uses AlarmManager to create an alarm by playing a sound.我正在创建一个 android 应用程序,它使用AlarmManager通过播放声音来创建警报。 To do this, I first create a PendingIntent, for which I have to create a class called AlarmReceiver , which extends BroadcastReceiver .为此,我首先创建一个 PendingIntent,为此我必须创建一个名为AlarmReceiver的类,它扩展了BroadcastReceiver In this new class, I override the onReceive method, in which I also start the sound.在这个新类中,我覆盖了 onReceive 方法,我也在其中启动了声音。 What I have right now works.我现在所拥有的工作。 However, as part of a bigger project, I will be later getting some data from a database.但是,作为更大项目的一部分,我稍后将从数据库中获取一些数据。 Regarding my question, this data is not important;关于我的问题,这个数据并不重要; what's important is that, after analyzing all the data, it will all come down to a boolean variable, which will be true or false.重要的是,在分析完所有数据后,所有数据都将归结为一个布尔变量,该变量为真或假。 This variable will be in MainActivity and I want to access it in my BroadcastReceiver class to check it and if true, I would stop the music.这个变量将在MainActivity 中,我想在我的BroadcastReceiver类中访问它来检查它,如果为真,我会停止音乐。 I've checked many SO questions related to these, but I still haven't found a solution.我已经检查了许多与这些相关的 SO 问题,但我仍然没有找到解决方案。

The code for the MainActivity is: MainActivity 的代码是:

package com.example.alarmsound;

public class MainActivity extends AppCompatActivity {

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

    Calendar t = Calendar.getInstance();
    t.add(Calendar.SECOND, 5);

    Context context = this;
    AlarmManager alarmMgr;
    alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmMgr.set(AlarmManager.RTC_WAKEUP, t.getTimeInMillis(), pendingIntent);
    boolean result; //the variable I want to access to BroadcastReceiver class
}
}

And the code for the BroadcastReceiver class is: BroadcastReceiver 类的代码是:

public class AlarmReceiver extends BroadcastReceiver{
public AlarmReceiver() {}

@Override
public void onReceive(Context context, Intent intent) {
    final MediaPlayer mp = MediaPlayer.create(context, R.raw.music);
    Log.d("Music", "It went here.");
    mp.start();

    //here, I want to access result
}
}

I would really appreciate any help.我真的很感激任何帮助。

In MainActivity:在主活动中:

Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("some_constant", result);

In your broadcast receiver:在您的广播接收器中:

boolean result = intent.getBooleanExtra("some_constant", false);

Create an instance of your MainActivity and get it using a method, add following code in your MainActivity :创建MainActivity一个实例并使用方法获取它,在MainActivity添加以下代码:

private static MainActivity instance;

@Override
protected void onStart() { // onStart() of your activity
    super.onStart();
    instance = this;
}

public void yourMethod(){
    // Your code here
}

public static MainActivity getInstance(){
    return instance;
}

Now, in your BroadcastReceiver , you can get the instance using:现在,在您的BroadcastReceiver ,您可以使用以下方法获取实例:

MainActivity obj = MainActivity.getInstance();

And you can call your method using:您可以使用以下方法调用您的方法:

obj.yourMethod();

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

相关问题 如何访问自定义线程池执行器方法中的线程对象? - How to access thread objects inside custom threadpool executor's methods? 如何从另一个类访问对象方法? - How can I access an objects methods from another class? 我如何从另一个 class 访问 MainActivity object - How can i access a MainActivity object from another class 无法从我的MainActivity访问另一个类中的方法 - Can't access methods in another class from my MainActivity 如何从 AlarmManager BroadcastReceiver 调用 MainActivity 方法? - How to call MainActivity method from AlarmManager BroadcastReceiver? 如何在一个子类中的不同方法中调用超类的几个对象? - How can I call few objects of superclass inside different methods in one subclass? 使用Android服务的TCP套接字。 我如何在MainActivity中声明流对象 - TCP Socket using Android service. how can i declare stream objects in MainActivity 学习如何在类内部创建对象和访问方法。 - learning to create object and access methods inside the class how can i..? 如何在循环内返回一个值,以便其他方法可以访问它? - How do I return a value inside the loop so that other methods can access it? 如何访问子类中的方法 - How can I access methods in a subclass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM