[英]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.