我有這個類,我已經創建,我需要把我最後的位置?權限不工作後的GPS位置(SDK 23)android studio
public class DescriptionActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks {
TextView t;
Button up;
GoogleApiClient mGoogleApiClient;
LocationManager locationManager;
LocationListener locationListener;
LocationServices mLastLocation;
private static final int PERMS_REQUEST_CODE=123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hr__description);
Toolbar toolbar = (Toolbar) findViewById(R.id.hr_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_clear_white_24dp);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
t = (TextView) findViewById(R.id.test_boaz);
up=(Button)findViewById(R.id.upload);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) this)
.build();
if(hasPermission())
{
getGPS();
}
else {
requestPerms();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case PERMS_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// All good!
} else {
Log.d("bobbbbbbbbbbbbbbbbbb","boazboabzobaob");
}
break;
}
}
private void getGPS() {
up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
private boolean hasPermission(){
int res=0;
String[] permissions=new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION};
for(String params:permissions)
{
res=checkCallingOrSelfPermission(params);
if(!(res==PackageManager.PERMISSION_GRANTED)) {
return false;
}
}
return true;
}
private void requestPerms(){
String[] permissions=new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION};
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
requestPermissions(permissions,PERMS_REQUEST_CODE);
ActivityCompat.requestPermissions(this, permissions, PERMS_REQUEST_CODE);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
}
}
@Override
public void onConnectionSuspended(int i) {
}
}
我試圖激活GPS,它總是要我檢查權限 如何解決呢? 看着在互聯網上很多例子,我已經成功獲得對話框來激活GPS設備,但是當我試圖讓位置,如果你想在谷歌的API之一的連接提供 我有問題
您可以發佈錯誤對話框嗎? – NilayDani
我沒有任何錯誤,只是他問我添加權限 –
哪個權限? – NilayDani