kopia lustrzana https://github.com/ge0rg/aprsdroid
fixed position: use AlarmManager for reliable wakeups
rodzic
b0296394ae
commit
b379871394
|
@ -1,31 +1,61 @@
|
||||||
package org.aprsdroid.app
|
package org.aprsdroid.app
|
||||||
|
|
||||||
|
import _root_.android.app.AlarmManager
|
||||||
|
import _root_.android.app.PendingIntent
|
||||||
|
import _root_.android.content.BroadcastReceiver
|
||||||
import _root_.android.content.Context
|
import _root_.android.content.Context
|
||||||
|
import _root_.android.content.Intent
|
||||||
|
import _root_.android.content.IntentFilter
|
||||||
import _root_.android.location.Location
|
import _root_.android.location.Location
|
||||||
import _root_.android.os.{Bundle, Handler}
|
import _root_.android.os.{Bundle, Handler}
|
||||||
import _root_.android.util.Log
|
import _root_.android.util.Log
|
||||||
|
|
||||||
class FixedPosition(service : AprsService, prefs : PrefsWrapper) extends LocationSource {
|
class FixedPosition(service : AprsService, prefs : PrefsWrapper) extends LocationSource {
|
||||||
val TAG = "APRSdroid.FixedPosition"
|
val TAG = "APRSdroid.FixedPosition"
|
||||||
val periodicPoster = new Runnable() { override def run() { postPosition(); postRefresh(); } }
|
val ALARM_ACTION = "org.aprsdroid.app.FixedPosition.ALARM"
|
||||||
|
|
||||||
|
val intent = new Intent(ALARM_ACTION)
|
||||||
|
val pendingIntent = PendingIntent.getBroadcast(service, 0, intent,
|
||||||
|
PendingIntent.FLAG_UPDATE_CURRENT)
|
||||||
|
|
||||||
|
// get called on alarm
|
||||||
|
val receiver = new BroadcastReceiver() {
|
||||||
|
override def onReceive(ctx : Context, i : Intent) {
|
||||||
|
Log.d(TAG, "onReceive: " + i)
|
||||||
|
postPosition()
|
||||||
|
postRefresh()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val manager = service.getSystemService(Context.ALARM_SERVICE).asInstanceOf[AlarmManager]
|
||||||
|
|
||||||
|
var alreadyRunning = false
|
||||||
|
|
||||||
override def start(singleShot : Boolean) = {
|
override def start(singleShot : Boolean) = {
|
||||||
|
if (alreadyRunning)
|
||||||
stop()
|
stop()
|
||||||
|
alreadyRunning = true
|
||||||
|
|
||||||
|
service.registerReceiver(receiver, new IntentFilter(ALARM_ACTION))
|
||||||
postPosition()
|
postPosition()
|
||||||
if (!singleShot)
|
if (!singleShot)
|
||||||
postRefresh()
|
postRefresh()
|
||||||
|
|
||||||
|
|
||||||
service.getString(R.string.p_source_manual)
|
service.getString(R.string.p_source_manual)
|
||||||
}
|
}
|
||||||
|
|
||||||
override def stop() {
|
override def stop() {
|
||||||
service.handler.removeCallbacks(periodicPoster)
|
manager.cancel(pendingIntent)
|
||||||
|
service.unregisterReceiver(receiver)
|
||||||
}
|
}
|
||||||
|
|
||||||
def postRefresh() {
|
def postRefresh() {
|
||||||
// get update interval
|
// get update interval
|
||||||
val upd_int = prefs.getStringInt("interval", 10)
|
val upd_int = prefs.getStringInt("interval", 10)
|
||||||
service.handler.postDelayed(periodicPoster, upd_int*60*1000)
|
Log.d(TAG, "postRefresh(): " + upd_int + " min")
|
||||||
|
manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + upd_int*60*1000,
|
||||||
|
pendingIntent)
|
||||||
}
|
}
|
||||||
|
|
||||||
def postPosition() {
|
def postPosition() {
|
||||||
|
@ -34,4 +64,5 @@ class FixedPosition(service : AprsService, prefs : PrefsWrapper) extends Locatio
|
||||||
location.setLongitude(prefs.getStringFloat("manual_lon", 0))
|
location.setLongitude(prefs.getStringFloat("manual_lon", 0))
|
||||||
service.postLocation(location)
|
service.postLocation(location)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue