我可以从服务内的共享首选项中获取数据吗? [英] Can I get data from shared preferences inside a service?

查看:111
本文介绍了我可以从服务内的共享首选项中获取数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个android应用程序.我正在使用android 2.2

I'm developing an android application. I'm using android 2.2

在我的应用程序中,我正在捕获GPS数据并将其以1小时的时间间隔发送到服务中.如果用户退出应用程序,它也可以正常工作(必需).

In my application I am capturing GPS data and sending it to service with the 1 hour time interval. If user exits from application it's also working (it is required).

我正在使用2种服务(用户定义),一种用于捕获GPS数据,另一种用于发送到服务器.

I'm using 2 services (User defined), one for capturing GPS data and other for sending to the server.

这是我的疑问

  • 在使用中,我们可以使用共享首选项吗?

  • In service, can we use shared preferences.

如果我们在应用程序的任何活动中将任何数据存储在共享首选项中,是否可以在共享首选项的帮助下在服务中使用该数据?

If we store any data in shared preferences in any activity of the application, will we be able to use that data in service with the help of shared preferences?

推荐答案


我找到了解决方案.
在服务内部,我们调用以下方法来获取共享的首选项


I find the solution.
Inside a service we call the following method to get the shared preferences

myapp.bmodel.getApplicationContext().getSharedPreferences("myPrefs_capture_gps_per_hour", Context.MODE_PRIVATE);


在上面的代码中, myapp 是从 Application

这篇关于我可以从服务内的共享首选项中获取数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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