2016-11-23 5 views
0

に失敗します、私は最後の位置を取得するための簡単なコードを以下している:依存関係のためは、常に接続がエラー

build.gardleファイル:マニフェストファイルで

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 24 
    buildToolsVersion "25.0.0" 
    defaultConfig { 
     applicationId "com.example.naresh2575.location" 
     minSdkVersion 15 
     targetSdkVersion 24 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 
    compile 'com.android.support:appcompat-v7:24.2.1' 
    testCompile 'junit:junit:4.12' 
    compile 'com.google.android.gms:play-services:10.0.0' 
} 

を、私は次の権限に

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

を使用しています。ここに私の活動のファイルです:

public class MainActivity 
     extends AppCompatActivity 
     implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener 
{ 

    GoogleApiClient mGoogleApiClient; 
    Location mLastLocation; 
    TextView tvLatLong; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     tvLatLong = (TextView) findViewById(R.id.tvLatLong); 
     buildGoogleApiClient(); 
    } 

    protected void onStart() { 
     mGoogleApiClient.connect(); 
     super.onStart(); 
    } 

    protected void onStop() { 
     mGoogleApiClient.disconnect(); 
     super.onStop(); 
    } 

    protected synchronized void buildGoogleApiClient() 
    { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
    } 

    public void onConnected(Bundle connectionHint) 
    { 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
     { 
      return; 
     } 
     mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     if (mLastLocation != null) 
     { 
      tvLatLong.setText("Latitude: " + String.valueOf(mLastLocation.getLatitude()) + " Longitude: " + String.valueOf(mLastLocation.getLongitude())); 
     } 
     else 
     { 
      tvLatLong.setText("mLastLocation is null"); 
     } 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     Toast.makeText(this, "Connection suspended...", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
     Toast.makeText(this, "Failed to connect...", Toast.LENGTH_SHORT).show(); 
    } 
} 

は、上記のコードは非常に簡単ですが、私は自分のアプリケーションを実行すると、私はいつも「接続に失敗しました...」というメッセージで書かを得るをonConnectionFailed。つまり、私はGoogle Playサービスに接続できません。私はそれを探す必要があるのですか?

答えて

0

Google開発者コンソールでSHA-1キーを使用してプロジェクトを作成しようとしましたか?プロジェクトの作成後、コンソールから認証キーを処理できます。この認証キーを使用することで、この問題を解決できます。