2016-11-07 96 views
0

我有這個類,我已經創建,我需要把我最後的位置?權限不工作後的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之一的連接提供 我有問題

+0

您可以發佈錯誤對話框嗎? – NilayDani

+0

我沒有任何錯誤,只是他問我添加權限 –

+0

哪個權限? – NilayDani

回答

0

在Google Play服務庫(例如Google登錄,遊戲或 雲端硬盤)中,您需要創建一個GoogleApiClient實例(「Google API 客戶端」)。 Google API客戶端爲所有Google Play服務提供了所有 的通用入口點,並管理用戶設備與每個Google服務之間的網絡連接,網址爲 。

  • 自動管理,以谷歌Play服務的連接。
  • 對任何Google Play服務執行同步和異步API調用。

  • 手動管理您在Google Play服務中的連接 極少數情況下,這是必要的。

添加到這個清單文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.google.android.gms.location.sample.basiclocationsample" > 

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
</manifest> 

添加該代碼在你的活動

mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(Drive.API) 
      .addScope(Drive.SCOPE_FILE) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .build() 
@Override 
    public void onConnected(Bundle connectionHint) { 
     mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
       mGoogleApiClient); 
     if (mLastLocation != null) { 
      mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude())); 
      mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude())); 
     } 
    } 
+0

,我需要插入此代碼?在OnCreate方法? –

+0

我沒有Drive.API它不識別 –

+0

當我添加此代碼 public void onConnected(@Nullable Bundle bundle){0} {0} mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 如果(mLastLocation!= NULL){ }} 我 –

0

在onCrea te在您的活動中的方法

+0

我沒有Drive.API無法識別 –