How can I access an Activity from another class globally in an Android app?
I have an Android app with 2 activities defined below. In the MainMenu.oncreate()
, I have an AlarmManager
kicked off to periodically query a server for data and update the text of a button in the PlayBack
UI. Can I access the Playback
object via a global reference or do I need to kick off the AlarmManager
in the Playback.oncreate()
instead so I can pass a reference to it? If so, should this be done with a BroadcastReceiver
and Intent
as I'm doing in the MainMenu
shown below?
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainMenu"
android:label="@string/app_name">
</activity>
<activity android:name=".Playing" android:label="@string/playing_title">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".NotificationUpdateReceiver" android:process=":remote" />
<service android:name="org.chirpradio.mobile.PlaybackService"
public class MainMenu extends Activity implements OnClickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
View playingButton = findViewById(R.id.playing_button);
playingButton.setOnClickListener(this);
try {
Long firstTime = SystemClock.elapsedRealtime();
// create an intent that will call NotificationUpdateReceiver
Intent intent = new Intent(this, NotificationUpdateReceiver.class);
// create the event if it does not exist
PendingIntent sender = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// call the receiver every 10 seconds
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.s开发者_运维知识库etRepeating(AlarmManager.ELAPSED_REALTIME, firstTime, 10000, sender);
} catch (Exception e) {
Log.e("MainMenu", e.toString());
}
}
}
I have an Android app with 2 activities defined below.
You only have one activity.
In the MainMenu.oncreate(), I have an AlarmManager kicked off to periodically query a server for data and update the text of a button in the PlayBack UI.
Why? Do you intend for these alarms to go on even after the user exits out of the activity?
Can I access the Playback object via a global reference or do I need to kick off the AlarmManager in the Playback.oncreate() instead so I can pass a reference to it?
Neither.
Using AlarmManager
means that you want the periodic work to continue even after the user exits the activity. Hence, it is very likely that there is no "Playback object", since the user probably is not in your activity. Your service can send its own broadcast Intent
to be picked up if the Playback
activity is still around. This sample project demonstrates using an ordered broadcast for this, so that if the activity is not around, a Notification
is raised instead.
If, on the other hand, you do not want the periodic work to continue if the user gets out of the activity, then do not use AlarmManager
. Use postDelayed()
within the activity, using a Runnable
that triggers your service via startService()
, then reschedules itself via postDelayed()
. In this case, you can consider using something like a Messenger
as a way to have the service let the activity know what is going on, if the activity is still around. This sample project demonstrates the use of a Messenger
in this fashion.
精彩评论