我需要在ComplexPreferences
Service
代码
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
sendRequestToServer();
}
private void sendRequestToServer() {
// How to get context here to initialize ctx ?
ComplexPreferences complexPreferences =
ComplexPreferences.getComplexPreferences( ctx , "App_Settings", 0);
}
怎么做?
课程的完整代码
public class gps_service extends Service {
private static final String TAG = "MyService";
private LocationListener listener;
private LocationManager locationManager;
private Timer timer = new Timer();
private DLocation dLocation;
private final Object lock = new Object();
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LocalService", "Received start id " + startId + ": " + intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
sendRequestToServer();
}
private void sendRequestToServer() {
synchronized (lock) {
try{
if(dLocation == null) return;
ComplexPreferences complexPreferences = ComplexPreferences.getComplexPreferences( context , "App_Settings", 0);
AppSettings appSettings = complexPreferences.getObject("App_Settings", AppSettings.class);
if(appSettings != null)
{
}
DLocation tempDL = dLocation;
LocationItem locationItem = new LocationItem();
locationItem.DeviceID = tempDL.DeviceID;
locationItem.Latitude = tempDL.Latitude;
locationItem.Longitude = tempDL.Longitude;
locationItem.TimeOfRequest = tempDL.TimeOfRequest;
Gson gson = new Gson();
String requestObject = gson.toJson(locationItem);
String url = "http://XXXXXXX";
makeRequest(url, requestObject);
}
catch (Exception ex)
{
}
}
}
}, 0, 1*60*1000); //1 Minutes
答案 0 :(得分:2)
试试这个
ComplexPreferences complexPreferences = ComplexPreferences.getComplexPreferences( YourService.this , "App_Settings", 0);
或者这个
Context context = this;
ComplexPreferences complexPreferences = ComplexPreferences.getComplexPreferences( context, "App_Settings", 0);
修改强>
public class gps_service extends Service {
private static final String TAG = "MyService";
private LocationListener listener;
private LocationManager locationManager;
private Timer timer = new Timer();
private DLocation dLocation;
private final Object lock = new Object();
Context context ;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LocalService", "Received start id " + startId + ": " + intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
context = this;
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
sendRequestToServer();
}
private void sendRequestToServer() {
synchronized (lock) {
try{
if(dLocation == null) return;
ComplexPreferences complexPreferences = ComplexPreferences.getComplexPreferences( context , "App_Settings", 0);
AppSettings appSettings = complexPreferences.getObject("App_Settings", AppSettings.class);
if(appSettings != null)
{
}
DLocation tempDL = dLocation;
LocationItem locationItem = new LocationItem();
locationItem.DeviceID = tempDL.DeviceID;
locationItem.Latitude = tempDL.Latitude;
locationItem.Longitude = tempDL.Longitude;
locationItem.TimeOfRequest = tempDL.TimeOfRequest;
Gson gson = new Gson();
String requestObject = gson.toJson(locationItem);
String url = "http://XXXXXXX";
makeRequest(url, requestObject);
}
catch (Exception ex)
{
}
}
}
}, 0, 1*60*1000); //1 Minutes