简体   繁体   English

如何在Android应用程序中全局访问另一个班级的活动?

[英]How can I access an Activity from another class globally in an Android app?

I have an Android app with 2 activities defined below. 我有一个Android应用,下面定义了2个活动。 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. MainMenu.oncreate() ,我启动了AlarmManager ,以定期查询服务器的数据并更新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? 我可以通过全局引用访问Playback对象,还是需要在Playback.oncreate()启动AlarmManager ,以便可以将引用传递给它? If so, should this be done with a BroadcastReceiver and Intent as I'm doing in the MainMenu shown below? 如果是这样,是否应该像在下面显示的MainMenu中那样使用BroadcastReceiverIntent来完成?

<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.setRepeating(AlarmManager.ELAPSED_REALTIME, firstTime, 10000, sender);

       } catch (Exception e) {
            Log.e("MainMenu", e.toString());
       }      
    }
}

I have an Android app with 2 activities defined below. 我有一个Android应用,下面定义了2个活动。

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. 在MainMenu.oncreate()中,我启动了AlarmManager,以定期向服务器查询数据并更新“播放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? 我可以通过全局引用访问Playback对象,还是需要在Playback.oncreate()中启动AlarmManager,以便可以将引用传递给它?

Neither. 都不行

Using AlarmManager means that you want the periodic work to continue even after the user exits the activity. 使用AlarmManager意味着即使用户退出活动后,您也希望继续进行定期工作。 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. 如果“ Playback活动仍在进行中,则您的服务可以发送自己的广播Intent以进行接收。 This sample project demonstrates using an ordered broadcast for this, so that if the activity is not around, a Notification is raised instead. 此示例项目演示了为此使用有序广播的方法,因此,如果活动不在附近,则会发出Notification

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 . 另一方面,如果您不想在用户退出活动后继续进行定期工作,则不要使用AlarmManager Use postDelayed() within the activity, using a Runnable that triggers your service via startService() , then reschedules itself via postDelayed() . 在活动中使用postDelayed() ,使用Runnable通过startService()触发您的服务,然后通过postDelayed()重新安排自身的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. 在这种情况下,如果活动仍在进行中,您可以考虑使用Messenger类的方法让服务让活动知道发生了什么。 This sample project demonstrates the use of a Messenger in this fashion. 此示例项目演示了以这种方式使用Messenger的情况。

暂无
暂无

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

相关问题 如何从活动中访问方法并将其用于Android中的另一个活动? - How can I access a method from an activity and use it into another activity in Android? 如何从Android应用程序中的另一个Activity访问主Webview实例? - How do I access the main webview instance from another Activity in my Android app? 如何从Android的Activity类中引用另一个项目的layout / xml文件? - How can I refer to layout/xml files of another project from my Activity class in Android? 如何在扩展Activity的类中访问布局,该类将由另一个Activity子类化? - How can I access the layout in a class extending Activity that is to be subclassed by another Activity? 如何从任何 class 或服务访问另一个 class 或活动的变量? - How to access a variable from any class or service to another class or activity? 如何在Android中保存全局“列表”或至少将其实例化以用于任何活动? - How I can save a “list” of globally or at least instance it for use in any activity, in Android? How to move from one activity to another ie Java Class to Kotlin Class in an Android Java Project? - How to move from one activity to another i.e Java Class to Kotlin Class in an Android Java Project? Android - 如何将ImageView从一个活动转移到另一个活动? - Android - how can i transfer ImageView from one activity to another activity? 如何在一个活动中将图像从ImageView发送到另一个活动中的ImageButton。 Android Studio - How can I send an image from an ImageView in one activity to an ImageButton in another activity. Android Studio 我如何通过使用Intent从Android中的另一个常规活动中调用特定的选项卡活动 - How can i call a specific tab activity from another regular activity in android by using intent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM