I m new to Anders and I m with the following problem. 我写了一份样品申请,其中我有一份意图服务,首先检查所有地点供应商,以找到最后的知道地点。 如果最后几个地点都没有提供足够准确(或及时)的地点,则要求地点主管采用广播用户的更新方法。 每次广播接收人使用收听方法时,都应检查地点,看看是否准确和/或足够及时。 我在打算服务中还有一个时间表,该服务最终会中断,并且应当检查是否获得准确和/或及时的足够地点更新。 问题Im在于,我不知道如何将广播接收器获得的地点数据重新输入意图服务。 象这样的景象应该容易做,但我已经过去了数天。 我认为,这样做的唯一途径是将数据写给广播接收器的一台ite,然后将这些记录重新读作意图服务,但似乎不必要地复杂。 是否有任何人知道将数据归还意图服务的适当途径? 我是否甚至应该利用广播接收器要求广播更新? 这样做是否容易? 这里是法典
public class GetLocationService extends IntentService {
public GetLocationService() {
super("something");
}
LocationManager locationManager;
long maxFixLateness;
float maxFixPosUncertainty;
boolean usableLocObtained;
Location bestLoc = null;
float bestLocScore = 0;
Intent locChangeI;
PendingIntent pLocChangeI;
@Override
final protected void onHandleIntent(Intent intent) {
maxFixLateness = 30000;
maxFixPosUncertainty = 30;
long curTime = System.currentTimeMillis();
LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
// Check for a usable location fix
List<string> matchingProviders = locationManager.getAllProviders();
for (String provider : matchingProviders) {
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
// ...some code to check if the location is accurate or timely
// enough
}
}
if (bestLoc == null) {
locChangeI = new Intent(this, HandleLocationUpdateReceiver.class);
pLocChangeI = PendingIntent.getBroadcast(this, 0, locChangeI,
PendingIntent.FLAG_UPDATE_CURRENT);
usableLocObtained = false;
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, pLocChangeI);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, pLocChangeI);
// Call the timer that will periodically check to see if a usable
// location has been obtained.
new LocFixCheckTimer(60000, 30, 1000);
}
}
private class LocFixCheckTimer {
Timer timer;
long numChecks;
public LocFixCheckTimer(long initSearchTime, long maxRechecks,
long recheckFreq) {
numChecks = maxRechecks;
timer = new Timer();
// Wait 2 seconds before checking for a fix again
timer.schedule(new CheckLocTask(), initSearchTime, recheckFreq);
}
class CheckLocTask extends TimerTask {
public void run() {
if (numChecks > 0) {
if (usableLocObtained == true) {
// I want to use the location data obtained from the
// HandleLocationUpdateReceiver s onReceive method
// but I don t how to get that data here.
}
} else {
// Cancel the timer. We ve timed-out on searching
// for a usable location fix
timer.cancel();
}
--numChecks;
}
}
}
}
这里是广播接收器:
public class HandleLocationUpdateReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Location loc = (Location) intent.getExtras().get(LocationManager.KEY_LOCATION_CHANGED);
if (loc != null)
{
double lat = loc.getLatitude();
double lon = loc.getLongitude();
// Do some checking to see how accurate and timely the location is
// here and somehow get it back to the intent service.
}
}
}
得到帮助!