如何在Android上为不同的活动提供定位服务

如何在Android上为不同的活动提供定位服务,android,location,Android,Location,我目前正在开发一款Android应用程序,我是这个领域的新手。 我想做以下工作: 创建一个类,该类封装了所需的功能,以便让活动知道用户的位置并向该活动提供映射 这应实现以下目标: 我有一个连接到位置服务的类来获取信息,其他活动可以使用这个类来获取地图(片段)以及其他信息(lat-long等),以供编程使用 我搞不懂……我不想让我的东西变成活动本身 但似乎整个Play服务的Google Location API都依赖于它是一个零碎的活动(不管是什么) 有什么想法吗 PS:我需要保持对2.3.3的支

我目前正在开发一款Android应用程序,我是这个领域的新手。 我想做以下工作:

创建一个类,该类封装了所需的功能,以便让活动知道用户的位置并向该活动提供映射

这应实现以下目标:

我有一个连接到位置服务的类来获取信息,其他活动可以使用这个类来获取地图(片段)以及其他信息(lat-long等),以供编程使用

我搞不懂……我不想让我的东西变成活动本身 但似乎整个Play服务的Google Location API都依赖于它是一个零碎的活动(不管是什么)

有什么想法吗

PS:我需要保持对2.3.3的支持

更新 我实现了一个扩展SupportMapFragment(缩写)的类,但在GMS的错误处理方面遇到了问题。在我的Galaxy Note 3上运行良好,但emulator有一个较旧版本的GMS,这(由于当前糟糕的错误处理)最终会导致空指针异常:

public class LocationFragment extends SupportMapFragment implements GooglePlayServicesClient.ConnectionCallbacks, 
GooglePlayServicesClient.OnConnectionFailedListener, 
LocationListener {

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        view = inflater.inflate(R.layout.map_fragment, container, false);

        initilizeMap();
        configureLocationClient();

        locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);

        // Start with updates turned off
        updatesRequested = false;
        map.getUiSettings().setMyLocationButtonEnabled(true);

        locationClient = new LocationClient(activity.getBaseContext(), this, this);

        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = getActivity();

        checkGooglePlayServices();
    }

private boolean checkGooglePlayServices() {
        // Check that Google Play services is available
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity.getBaseContext());
        // If Google Play services is available
        if (ConnectionResult.SUCCESS == resultCode) {
            // In debug mode, log the status
            Log.d(TAG + ".servicesConnected()", "Google Play services is available.");
            // Continue
            return true;
        // Google Play services was not available for some reason
        } else {
            Log.d(TAG + ".servicesConnected()", "Google Play services is unavailable or outdated: " + resultCode);
            // Get the error dialog from Google Play services
            try {
                GooglePlayServicesUtil.getErrorDialog(resultCode, activity, CONNECTION_FAILURE_RESOLUTION_REQUEST).show();
            } catch (Exception e) {
                Log.e("Error: GooglePlayServiceUtil: ", "" + e);
            }
            /**
            Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(resultCode, activity, CONNECTION_FAILURE_RESOLUTION_REQUEST);

            // If Google Play services can provide an error dialog
            if (errorDialog != null) {
                // Create a new DialogFragment for the error dialog
                ErrorDialogFragment errorFragment = new ErrorDialogFragment();
                // Set the dialog in the DialogFragment
                errorFragment.setDialog(errorDialog);
                // Show the error dialog in the DialogFragment
                errorFragment.show(activity.getSupportFragmentManager(),"Location Updates");
            }*/

            return false;
        }
    }
相应的活动是:

public class LocationTest extends FragmentActivity implements LocationListener {
    private static final String TAG = LocationTest.class.getSimpleName();

    private int count = 0;

    private TextView lat;
    private TextView lng;
    private TextView quality;
    private TextView conState;
    private TextView refreshCount;
    private TextView distance;
    private LocationFragment locFrag;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.locationtest);

        FragmentManager fManager = getSupportFragmentManager();
        locFrag = new LocationFragment();
        FragmentTransaction ft = fManager.beginTransaction();
        ft.replace(R.id.map, locFrag);
        ft.addToBackStack(null);
        ft.commit();

        locFrag.setGoal(Double.valueOf(8.83749747285), Double.valueOf(53.0663656578));

        lat = (TextView) findViewById(R.id.curLat);
        lng = (TextView) findViewById(R.id.curLng);
        quality = (TextView) findViewById(R.id.curQuality);
        conState = (TextView) findViewById(R.id.conState);
        refreshCount = (TextView) findViewById(R.id.count);
        distance = (TextView) findViewById(R.id.distance);
    }

    public void refreshLocation(View v) {
        locFrag.refreshLocation(v);
    }
    public void toggleUpdates(View v) {
        locFrag.toggleUpdates(v);

    }

    public void onLocationChanged(Location location)  {
        // Let the location parent know about the location change
//      super.onLocationChanged(location);
        count++;
        lat.setText(Double.toString(locFrag.getCurrentLatitude()));
        lng.setText(Double.toString(locFrag.getCurrentLongitude())); 
        quality.setText(Float.toString(locFrag.getAccuracy()));
        conState.setText(Boolean.toString(locFrag.isConnected()));
        refreshCount.setText(Integer.toString(count));
        distance.setText(Float.toString(locFrag.getDistance()));
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart()");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause()");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop()");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume()");
    }
}
我想知道是否有可能让google建议的ErrorDialog从SupportMapFragment开始工作,以便能够处理错误,或者停止片段膨胀并返回到以前的活动


感谢您的帮助:)

在当前正在捕获位置的类中使用此代码

currentLocation是位置对象

Intent intent = new Intent(this,target.class)
Bundle b = new Bundle();
b.putParcelable("Location", currentLocation);
i.putExtra("Location", b);
startActivity(i);
接收活动代码:

b = getArguments();
Location location = b.getParcelable("Location");
这样你就可以通过这个位置。我想它可能对你有用