學習《第一行代碼》代碼筆記,此處學習的是第一版基於Eclipse的,但是Android Studio需要動態獲取權限,所以有些部分不兼容部分。因爲我把獲取經緯度放在了點擊事件裏,所以處理監聽位置改變的邏輯就不好插入進去,而且監聽也需要實例化location並且獲取權限。
關於回調函數,我也不是很明白,在調試的時候也沒有進入到這個方法裏去。暫且先保存在這裏,作爲一個學習記錄,同時以後要用的時候直接拿下來更改。
package com.hrd.jc.test2;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView positionTextView;
private LocationManager locationManager;
private String provider;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
positionTextView = (TextView) findViewById(R.id.postion_text_view);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providerList = locationManager.getProviders(true);//獲取所有可用的位置提供器
//傳入True表示只有啓用的位置提供器纔會被返回
if (providerList.contains(LocationManager.GPS_PROVIDER)) {
provider = locationManager.GPS_PROVIDER;
Log.i("權限log", "gps");
} else if (providerList.contains(locationManager.NETWORK_PROVIDER)) {
provider = locationManager.NETWORK_PROVIDER;
Log.i("權限log", "network");
} else {
Toast.makeText(this, "no location provider to use", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn) {
Log.i("權限log", "點擊了按鈕");
dosomething();
}
}
private void dosomething() {
//權限判斷和獲取
int permissionCheck = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
Log.i("權限log", "沒有權限");
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
Log.i("權限log", "拒絕聲明");
Toast.makeText(this, "u had rejected the request", Toast.LENGTH_SHORT).show();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
} else {//如果有權限,就直接進行事件處理
Log.i("權限log", "有權限");
getMylocation();
}
}
public void getMylocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
} else {
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
Log.i("權限log", "顯示位置");
showLocation(location);//自定義方法
}
}
// locationManager.requestLocationUpdates(provider, 5000, 1, locationListener);
//監聽位置變化,全局,但是我在此處把查詢寫成了點擊事件,所以只在點擊後響應,後續解決
//位置變化之後,觸發show()方法。但是隻有點擊之後纔有。,所以每次點擊之後,滿足兩次變化監聽之後,會tost兩次
//如果要做一個實時位置信息,考慮設置一個循環,每多長時間進行一次查詢,或者有變化了進行一次顯示。,
}
public void OnRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions,grantResults);
switch (requestCode) {
case 1:
if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
Log.i("權限log", "回調");
Toast.makeText(MainActivity.this, "回調 ", Toast.LENGTH_SHORT).show();
getMylocation();
} else {
// Permission Denied
Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
return;
}
}
//位置變化監聽
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {//更新當前的位置信息
showLocation(location);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
private void showLocation(Location location) {
String currentPosition = "latitude is" + location.getLatitude() + "\n" + "longitude is" + location.getLongitude();
positionTextView.setText(currentPosition);
Toast.makeText(this,"刷新了位置信息",Toast.LENGTH_SHORT).show();
}
protected void onDestroy() {
super.onDestroy();
if (locationManager != null) {
//關閉程序將監聽器移除
locationManager.removeUpdates(locationListener);
}
}
}
xml文件只有一個textView和一個button,id上面可看到,此處就不寫了。
對於我的問題,如果有大神指點我將感激不盡,如果有同樣的初學者,可以一起相互交流促進。