我能得到一个服务中从共享preferences数据 [英] Can i get data from shared preferences inside a service

查看:305
本文介绍了我能得到一个服务中从共享preferences数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序。我使用的Andr​​oid 2.2

在我的应用我获取GPS数据和发送它与1小时的时间间隔来服务。如果用户从应用程序它也工作(这是必需的)。

退出

我使用2服务(用户定义的),一个用于捕获GPS数据等发送给服务器。

下面我怀疑


  • 在服务,我们可以使用共享prefrences。


  • 如果我们存储在应用程序的任何活动共享preferences任何数据,我们将能够使用数据服务与共享preferences的帮助?


欢迎各界人士来给自己的想法。


解决方案

结果我找到解决方案。
结果里面一个服务我们调用下面的方法来获取共享preferences

<$p$p><$c$c>myapp.bmodel.getApplicationContext().getShared$p$pferences(\"my$p$pfs_capture_gps_per_hour\", Context.MODE_PRIVATE);

结果在上面的code 的myapp <​​/ STRONG>是源自应用程序类的一个对象的应用

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

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).

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

Here my doubt

  • In service, can we use shared prefrences.

  • 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?

All are welcome to give their ideas.

解决方案


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);


In the above code myapp is a object of the application class which is derived from Application

这篇关于我能得到一个服务中从共享preferences数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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