package com.anydata.android.widget.weatherclock;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.os.Looper;
public class AutoFindLocationService extends Service implements Runnable{
Thread thread = null;
@Override
public IBinder onBind(Intent arg0) {
System.out.println("AutoFindLocationService :: onBind");
return null;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
System.out.println("AutoFindLocationService :: onStart");
/*AppWidgetManager manager = AppWidgetManager.getInstance(this);
// If requested, trigger update of all widgets
if (ACTION_UPDATE_ALL.equals(intent.getAction())) {
Log.d(TAG, "Requested UPDATE_ALL action");
// AppWidgetManager manager = AppWidgetManager.getInstance(this);
requestUpdate(manager.getAppWidgetIds(new ComponentName(this,
WeatherForecastAppWidget.class)));
}
// Only start processing thread if not already running
synchronized (sLock) {
if (!sThreadRunning) {
sThreadRunning = true;
new Thread(this).start();
}
}*/
if(thread == null || thread.isAlive() == false)
{
thread = new Thread(this);
thread.run();
}
}
@Override
public boolean stopService(Intent intent){
boolean b = super.stopService(intent);
thread.destroy();
System.out.println("AutoFindLocationService :: stopService");
return b;
}
public void run() {
SharedPreferences config = this.getApplicationContext().getSharedPreferences(ConstData.PREF_FILE_NAME, 0);
Looper.prepare();
while(true){
try {
String originalCity = config.getString(ConstData.PREFS_CITY_FIELD_Key, ConstData.PREFS_CITY_KEY_DEFAULT);
System.out.println("originalCity: " + originalCity);
Thread.sleep(10000);
//TODO: auto set the current city via google api
String newCity = "Nanjing";
if(newCity.equalsIgnoreCase(originalCity) == false && newCity != null && newCity.length() >=1)
{
SharedPreferences.Editor editor = config.edit();
editor.putString(ConstData.PREFS_CITY_FIELD_Key, newCity);
editor.commit();
String s = config.getString(ConstData.PREFS_CITY_FIELD_Key, ConstData.PREFS_CITY_KEY_DEFAULT);
System.out.println("XXXXXXXXXXXXXXX: " + s);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("AutoFindLocationService is running......");
}
}
}
|