private LocationManager lm;
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean ok = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (ok) {//開了定位服務
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// 沒有權限,申請權限。
Toast.makeText(this, "沒有權限", Toast.LENGTH_SHORT).show();
} else {
// 有權限了,去放肆吧。
Toast.makeText(this, "有權限", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "系統(tǒng)檢測到未開啟GPS定位服務", Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 1315);
}