0
我希望每間隔5分鐘後通過後端服務或數據庫中的線程保存當前的經度和緯度。然後我從數據庫中搜索所有lat,並繪製它的位置路徑。每間隔5分鐘後通過後臺服務將緯度保存在數據庫中
我該如何在android中做到這一點?這是我的代碼:
public class MyTestService extends IntentService {
Intent i;
TimerTask hourlyTask;
Timer timer;
Location location;
// Must create a default constructor
public MyTestService() {
// Used to name the worker thread, important only for debugging.
super("test-service");
}
@Override
public void onCreate() {
super.onCreate(); // if you override onCreate(), make sure to call super().
// If a Context object is needed, call getApplicationContext() here.
onHandleIntent(i);
}
@Override
protected void onHandleIntent(Intent intent) {
timer = new Timer();
hourlyTask = new TimerTask() {
@Override
public void run() {
onHandleIntent(i);
}
};
// schedule the task to run starting now and then every hour...
timer.schedule(hourlyTask, 0l, 1000*5*60); // 1000*10*60 every 10 minut
SQLiteDatabase db;
db = openOrCreateDatabase("Temp.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
try {
final String CREATE_TABLE_CONTAIN = "CREATE TABLE IF NOT EXISTS tbl_Contain ("
+ "ID INTEGER primary key AUTOINCREMENT,"
+ "DESCRIPTION TEXT,"
+ "expirydate DATETIME,"
+ "AMOUNT TEXT,"
+ "TRNS TEXT," + "isdefault TEXT);";
db.execSQL(CREATE_TABLE_CONTAIN);
Toast.makeText(getApplicationContext(), "table created ", Toast.LENGTH_LONG).show();
String sql =
"INSERT or replace INTO tbl_Contain (DESCRIPTION, expirydate, AMOUNT, TRNS,isdefault) VALUES('farhan','03/04/2005','5000','ali','y')";
db.execSQL(sql);
Toast.makeText(getApplicationContext(), "inserted", Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
Toast.makeText(getApplicationContext(), "ERROR " + e.toString(), Toast.LENGTH_LONG).show();
}
}
感謝rply低於警報管理器訪問文檔鏈接@navin我得到緯度長值主要問題是,如何在DB通過後臺插入這些值服務 –
你可以使用ScheduledThreadPoolExecutor或其他選項在ScheduledThreadPoolExecutor例子的代碼中看到我的編輯,但你也可以使用AlarmManager或Timer。 –
什麼是ScheduledThreadPoolExecutor –