English 中文(简体)
甲型六氯环己烷
原标题:android development: get data from BroadcastReceiver called via requestLocationUpdates

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.
    }
  }
}

得到帮助!

问题回答

利用听众向您的活动或服务发送数据。 https://stackoverflow.com/q/7709140/779408。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签