为什么我的应用程序显示高电量使用?

时间:2015-06-23 19:51:22

标签: android performance

我正在使用谷歌融合位置api,但仍显示其高电池使用率。我正在开发一个应用程序,我需要获取用户当前位置,之后我不需要获取位置。我是如何获取当前位置

public class MyLocationManager implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener {

    private static MyLocationManager locationManager;
    private static LocationClient locationclient;

    public static final int LAT_KNOW_NLOCATION = 1005;
    public static final int CURRENT_LOCATION = 1006;
    public static final int CRON_LOCATION_UPDATE = 1007;

    private static String LOCATION_CLIENT_NOT_FOUND_MESSAGE = "Error... Location client not found!";

    private static Context context;
    private static Activity currentActivity;
    private static ProgressDialog progressDialog;
    private static ILocationUpdator iLocationUpdator;

    private static final int UPDATE_INTERVAL = 1000 * 30;
    private static LocationManager androidLocationManager;


    private static int CURRENTLY_LOOKING_FOR = 0;

    private MyLocationManager(Context context) {
        locationclient = new LocationClient(context, this, this);
        MyLocationManager.context = context;
        androidLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    }

    public static synchronized MyLocationManager getLocationManagerInstance(Context context, ILocationUpdator iLocationUpdator, Activity activity) {
        currentActivity = activity;
        if (locationManager == null) {
            locationManager = new MyLocationManager(context);
        }

        progressDialog = new ProgressDialog(currentActivity);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("Please wait looking for current location");

        MyLocationManager.iLocationUpdator = iLocationUpdator;
        return locationManager;
    }

    public void getCurrentLocation() {
        CURRENTLY_LOOKING_FOR = MyLocationManager.CURRENT_LOCATION;
        callForLocationUpdate();
    }

    public void getlastKnownLocation() {
        CURRENTLY_LOOKING_FOR = MyLocationManager.LAT_KNOW_NLOCATION;
        callForLocationUpdate();
    }


    public void getCronLocationUpdate() {
        CURRENTLY_LOOKING_FOR = MyLocationManager.CRON_LOCATION_UPDATE;
        callForLocationUpdate();
    }

    private void callForLocationUpdate() {
        System.out.println("callForLocationUpdate " + isProviderEnabled());
        if (isProviderEnabled()) {
            getReliventLocation(CURRENTLY_LOOKING_FOR);
        }
        else {
            DialogUtility.showCallbackMessage("Please turn on GPS/Location service.", currentActivity, new AlertDialogCallBack() {

                @Override
                public void onSubmit() {
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    currentActivity.startActivity(intent);
                }

                @Override
                public void onCancel() {

                }

                @Override
                public void onSubmitWithEditText(String text) {

                }
            });
        }

    }

    private boolean isProviderEnabled() {
        boolean gps_enabled = androidLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean network_enabled = androidLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (gps_enabled && network_enabled) {
            return true;
        }
        else {
            return false;
        }
    }


    private void getReliventLocation(int lookingFor) {
        Location location = null;
        if (locationclient != null) {
            currentActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    progressDialog.show();
                }
            });

            if (!locationclient.isConnected()) {
                locationclient.connect();
            }
            else {
                LocationRequest locationrequest = LocationRequest.create();
                locationrequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
                LocationListener locationListener = new LocationListener() {
                    @Override
                    public void onLocationChanged(Location location) {
                        sendLocationUpdate(location, this);
                    }
                };
                switch (lookingFor) {
                    case MyLocationManager.LAT_KNOW_NLOCATION:
                        location = locationclient.getLastLocation();
                        sendLocationUpdate(location, null);
                        break;

                    case MyLocationManager.CURRENT_LOCATION:
                        locationrequest.setInterval(UPDATE_INTERVAL);
                        locationclient.requestLocationUpdates(locationrequest, locationListener);
                        break;

                    case MyLocationManager.CRON_LOCATION_UPDATE:
                        Intent mIntentService = new Intent(context, LocationService.class);
                        PendingIntent mPendingIntent = PendingIntent.getService(context, 1, mIntentService, 0);
                        locationrequest.setInterval(100);
                        locationclient.requestLocationUpdates(locationrequest, mPendingIntent);
                        break;
                }
            }
        }
        else {
            showErrorMessage(LOCATION_CLIENT_NOT_FOUND_MESSAGE);
        }
    }

    private void sendLocationUpdate(Location location, LocationListener locationListener) {
        if (location != null) {
            double lat = location.getLatitude();
            double lon = location.getLongitude();
            iLocationUpdator.getLocation(new LatLng(lat, lon), 0, "success..");
        }
        else {
            iLocationUpdator.getLocation(new LatLng(0.0, 0.0), 1, "Some error occured..");
        }

        if (progressDialog != null) {
            progressDialog.dismiss();
        }

        if (CURRENTLY_LOOKING_FOR == MyLocationManager.CURRENT_LOCATION) {
            locationclient.removeLocationUpdates(locationListener);
        }
    }

    private static void showErrorMessage(String message) {
        Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        if (iLocationUpdator != null)
            iLocationUpdator.getLocation(new LatLng(0.0, 0.0), 1, "Connection Result...Error!");
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }

    @Override
    public void onConnected(Bundle bundle) {
        getReliventLocation(CURRENTLY_LOOKING_FOR);
    }

    @Override
    public void onDisconnected() {
        if (iLocationUpdator != null)
            iLocationUpdator.getLocation(new LatLng(0.0, 0.0), 1, "Connection Disconnected...Error!");
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }
}

请指出我做错了什么。

1 个答案:

答案 0 :(得分:0)

我不熟悉Android位置服务,但我认为您可以使用AT& T提供的免费工具诊断您的电池使用情况。

应用资源优化工具: https://developer.att.com/application-resource-optimizer

看起来您可能正在轮询当前位置更新,其中每个都会影响电池续航时间。 ATT ARO可以帮助识别这一点。