1 /* 2 * Copyright 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.powerprofile.gpson; 18 19 import android.app.Activity; 20 import android.content.Context; 21 import android.content.pm.PackageManager; 22 import android.location.Location; 23 import android.location.LocationManager; 24 import android.location.LocationListener; 25 import android.os.Bundle; 26 import android.support.v4.app.ActivityCompat; 27 import android.support.v4.content.ContextCompat; 28 import android.util.Log; 29 30 public class GpsActivity extends Activity { 31 32 private static final int LOCATION_INTERVAL = 1000; 33 private static final float LOCATION_DISTANCE = 0f; 34 private static final int ACCESS_FINE_LOCATION_PERMISSION = 0; 35 private LocationManager mLocationManager; 36 private LocationListener mLocationListener; 37 38 protected void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 41 int permissionsCheck = ContextCompat.checkSelfPermission(this, 42 android.Manifest.permission.ACCESS_FINE_LOCATION); 43 44 if (permissionsCheck != PackageManager.PERMISSION_GRANTED) { 45 ActivityCompat.requestPermissions(this, 46 new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 47 ACCESS_FINE_LOCATION_PERMISSION); 48 } 49 } 50 51 @Override 52 public void onRequestPermissionsResult(int requestCode, String permissions[], 53 int[] grantResults) { 54 switch (requestCode) { 55 case ACCESS_FINE_LOCATION_PERMISSION: { 56 if (grantResults.length > 0 57 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 58 59 initLocation(); 60 } 61 break; 62 } 63 } 64 } 65 66 private void initLocation() { 67 mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 68 69 mLocationListener = new LocationListener() { 70 public void onLocationChanged(Location location) {} 71 72 public void onStatusChanged(String provider, int status, Bundle extras) {} 73 74 public void onProviderEnabled(String provider) {} 75 76 public void onProviderDisabled(String provider) {} 77 }; 78 } 79 80 @Override 81 protected void onResume() { 82 super.onResume(); 83 84 if (mLocationManager == null || mLocationListener == null) { 85 return; 86 } 87 88 try { 89 mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 90 LOCATION_INTERVAL, LOCATION_DISTANCE, mLocationListener); 91 } catch (SecurityException e) { 92 e.printStackTrace(); 93 } 94 } 95 96 @Override 97 protected void onPause() { 98 super.onPause(); 99 100 if (mLocationManager == null || mLocationListener == null) { 101 return; 102 } 103 104 mLocationManager.removeUpdates(mLocationListener); 105 } 106 107 } 108