| /* |
| * Copyright 2017, 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.lifecycles.step4_solution; |
| |
| import android.Manifest; |
| import android.content.pm.PackageManager; |
| import android.location.Location; |
| import android.location.LocationListener; |
| import android.os.Bundle; |
| import android.support.annotation.NonNull; |
| import android.support.v4.app.ActivityCompat; |
| import android.widget.TextView; |
| import android.widget.Toast; |
| |
| import android.arch.lifecycle.LifecycleActivity; |
| import com.example.android.codelabs.lifecycle.R; |
| |
| public class LocationActivity extends LifecycleActivity { |
| |
| private static final int REQUEST_LOCATION_PERMISSION_CODE = 1; |
| |
| private LocationListener mGpsListener = new MyLocationListener(); |
| |
| @Override |
| public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, |
| @NonNull int[] grantResults) { |
| super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
| if (grantResults[0] == PackageManager.PERMISSION_GRANTED |
| && grantResults[1] == PackageManager.PERMISSION_GRANTED) { |
| bindLocationListener(); |
| } else { |
| Toast.makeText(this, "This sample requires Location access", Toast.LENGTH_LONG).show(); |
| } |
| } |
| |
| private void bindLocationListener() { |
| BoundLocationManager.bindLocationListenerIn(this, mGpsListener, getApplicationContext()); |
| } |
| |
| @Override |
| protected void onCreate(Bundle savedInstanceState) { |
| super.onCreate(savedInstanceState); |
| setContentView(R.layout.location_activity); |
| |
| if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) |
| != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, |
| Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { |
| ActivityCompat.requestPermissions(this, |
| new String[]{Manifest.permission.ACCESS_FINE_LOCATION, |
| Manifest.permission.ACCESS_COARSE_LOCATION}, |
| REQUEST_LOCATION_PERMISSION_CODE); |
| } else { |
| bindLocationListener(); |
| } |
| } |
| |
| private class MyLocationListener implements LocationListener { |
| @Override |
| public void onLocationChanged(Location location) { |
| TextView textView = (TextView) findViewById(R.id.location); |
| textView.setText(location.getLatitude() + ", " + location.getLongitude()); |
| } |
| |
| @Override |
| public void onStatusChanged(String provider, int status, Bundle extras) { |
| } |
| |
| @Override |
| public void onProviderEnabled(String provider) { |
| Toast.makeText(LocationActivity.this, |
| "Provider enabled: " + provider, Toast.LENGTH_SHORT).show(); |
| } |
| |
| @Override |
| public void onProviderDisabled(String provider) { |
| } |
| } |
| } |