diff options
Diffstat (limited to 'power_profile/gps_on/Application/src/main/java/com/example/android/powerprofile/gpson/GpsActivity.java')
-rw-r--r-- | power_profile/gps_on/Application/src/main/java/com/example/android/powerprofile/gpson/GpsActivity.java | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/power_profile/gps_on/Application/src/main/java/com/example/android/powerprofile/gpson/GpsActivity.java b/power_profile/gps_on/Application/src/main/java/com/example/android/powerprofile/gpson/GpsActivity.java new file mode 100644 index 00000000..ef68ad81 --- /dev/null +++ b/power_profile/gps_on/Application/src/main/java/com/example/android/powerprofile/gpson/GpsActivity.java @@ -0,0 +1,107 @@ +/* + * Copyright 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.powerprofile.gpson; + +import android.app.Activity; +import android.content.Context; +import android.content.pm.PackageManager; +import android.location.Location; +import android.location.LocationManager; +import android.location.LocationListener; +import android.os.Bundle; +import android.support.v4.app.ActivityCompat; +import android.support.v4.content.ContextCompat; +import android.util.Log; + +public class GpsActivity extends Activity { + + private static final int LOCATION_INTERVAL = 1000; + private static final float LOCATION_DISTANCE = 0f; + private static final int ACCESS_FINE_LOCATION_PERMISSION = 0; + private LocationManager mLocationManager; + private LocationListener mLocationListener; + + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + int permissionsCheck = ContextCompat.checkSelfPermission(this, + android.Manifest.permission.ACCESS_FINE_LOCATION); + + if (permissionsCheck != PackageManager.PERMISSION_GRANTED) { + ActivityCompat.requestPermissions(this, + new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, + ACCESS_FINE_LOCATION_PERMISSION); + } + } + + @Override + public void onRequestPermissionsResult(int requestCode, String permissions[], + int[] grantResults) { + switch (requestCode) { + case ACCESS_FINE_LOCATION_PERMISSION: { + if (grantResults.length > 0 + && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + + initLocation(); + } + break; + } + } + } + + private void initLocation() { + mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); + + mLocationListener = new LocationListener() { + public void onLocationChanged(Location location) {} + + public void onStatusChanged(String provider, int status, Bundle extras) {} + + public void onProviderEnabled(String provider) {} + + public void onProviderDisabled(String provider) {} + }; + } + + @Override + protected void onResume() { + super.onResume(); + + if (mLocationManager == null || mLocationListener == null) { + return; + } + + try { + mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, + LOCATION_INTERVAL, LOCATION_DISTANCE, mLocationListener); + } catch (SecurityException e) { + e.printStackTrace(); + } + } + + @Override + protected void onPause() { + super.onPause(); + + if (mLocationManager == null || mLocationListener == null) { + return; + } + + mLocationManager.removeUpdates(mLocationListener); + } + +} |