在活动,服务和活动之间传递变量android中的应用程序 [英] Passing variables between activities, services & applications in android

查看:40
本文介绍了在活动,服务和活动之间传递变量android中的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请有人为我提供以下活动/服务/应用程序"组合的示例.我三个都有,但是我已经把我的应用程序弄得一团糟,试图在周围传递一堆变量,现在我不知道发生了什么.请注意,我是android的新手,最近我为此感到挣扎,因为可以通过多种方法来实现它.

Would someone please provide me an example of the following Activity/Service/Application combination. I have all three, but I've turned my app into such a mess trying to pass a bunch of variables around the place, and now I don't know what's going on. Please be aware that I am new to android, and I have recently struggled with this, as there are so many ways this can be implemented.

我只想看到以下三种活动,服务和应用程序类别:

I'd just like to see the 3 classes of activity, service and application where the following happens:

  • 活动将变量x存储在应用程序中,启动Service,然后启动活动2.

  • Activity stores variable x in Application, launches Service, and starts Activity 2.

服务从应用程序中检索变量x.

Service retrieves variable x from Application.

活动2从应用程序中检索变量x.

Activity 2 retrieves variable x from Application.

请注意,变量x可以是从Int到ArrayList的任何东西,并且在我的实际程序中,有很多变量(因此需要应用程序类).

Note that variable x could be anything from an Int to an ArrayList, and that in my actual program, there are a lot of variables (hence the desire for an application class).

我真的很感谢这个特定示例的一个很好的示例,因为我已经尝试了好一阵子了.如果有人愿意花时间整理一个可靠的答案,我将不胜感激.

I'd really appreciate a good example of this specific example, as I've been trying to figure all this out for a while. If someone would please take the time to put together a solid answer, I would greatly appreciate it.

对于任何问为什么的人,整个过程都是音乐播放器.用户选择一首歌曲,然后将艺术家/专辑等(希望)存储在应用程序中.然后启动该服务,该服务控制歌曲的播放,并从应用程序获取歌曲路径.第二个活动显示一个带有歌曲信息的UI(同样来自应用程序),并具有下一个/上一个"按钮,这些按钮将更改应用程序中某些变量的值,并指示服务检索新值.如果用户离开,变量将始终存在于应用程序中,因此,如果创建了另一个UI,则可以轻松设置歌曲信息.

For anyone asking why, the whole thing is a music player. The user picks a song, and the artist/album etc is (hopefully) stored in the application. Then the service is started, which controls the song playback, getting the songpath from the application. The second activity displays a UI with the song information (also from the application) and has next/previous buttons, which will change the value of some of the variables in the application, and instruct the service to retrieve the new values. If the user navigates away, the variables will always exist in the application, so if another UI is created, the song information can be set easily.

我使用正确的方法吗?

我可以举一个例子说明我所拥有的东西,但是目前这是一团糟.无论如何,如果您认为这将有助于您帮助我,请在下面要求.

I can provide an example of what I've got, but it is a mess at the moment. Anyhow, just request for that below if you think it will help you to help me.

推荐答案

您无需为此扩展Application.您可以在某些类中使用静态成员变量,如下所示:

You don't need to extend Application for this. You can just use static member variables in some class, like this:

public class Globals {
    public static String myVariable;
}

public class Activity1 extends Activity implements View.OnClickListener {
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ...
    }

    public void onClick(View view) {
        // Save song name and stuff
        Globals.myVariable = "SongNameAndStuff"; // Save in static global variable
        startService(); // with appropriate parameters
        startActivity(); // with appropriate parameters

    }
}

public class Activity2 extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // Get song name from Globals
       String mySongName = Globals.myVariable;
        ...
    }

public class MyService extends Service {
    // Access to song name from whatever method needs it:
    String mySongName = Globals.myVariable;
}

这篇关于在活动,服务和活动之间传递变量android中的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆