2016-11-12 88 views
2

我想使用android Wear應用程序獲取設備位置,但我的應用程序沒有響應,因爲它應該。 我不明白爲什麼系統不會輸入任何OnLocationChanged,OnConnectionFailed等功能... 我的猜測是,它無法連接到Google,因爲它應該但我不確定。歡迎任何幫助,謝謝!使用Android Wear獲取設備位置

public class MainWearActivity extends WearableActivity implements 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, 
     LocationListener { 

    private GoogleApiClient client; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 


     setAmbientEnabled(); 
     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
    } 

    GoogleApiClient googleApiClient; 

    // Connect to Google Play Services when the Activity starts 
    @Override 
    protected void onStart() { 
     super.onStart(); 
     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client.connect(); 
     if (googleApiClient == null) { 
      googleApiClient = new GoogleApiClient.Builder(this) 
        .addApi(LocationServices.API) 
        .addConnectionCallbacks(this) 
        .addOnConnectionFailedListener(this) 
        .build(); 
     } 
     googleApiClient.connect(); 
     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     Action viewAction = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "MainWear Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       // make sure this auto-generated web page URL is correct. 
       // Otherwise, set the URL to null. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app URL is correct. 
       Uri.parse("android-app://com.example.arturo.pingutest/http/host/path") 
     ); 
     AppIndex.AppIndexApi.start(client, viewAction); 
    } 

    // Register as a listener when connected 
    @Override 
    public void onConnected(Bundle connectionHint) { 

     // Create the LocationRequest object 
     LocationRequest locationRequest = LocationRequest.create(); 
     // Use high accuracy 
     locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     // Set the update interval to 2 seconds 
     locationRequest.setInterval(TimeUnit.SECONDS.toMillis(2)); 
     // Set the fastest update interval to 2 seconds 
     locationRequest.setFastestInterval(TimeUnit.SECONDS.toMillis(2)); 
     // Set the minimum displacement 
     locationRequest.setSmallestDisplacement(2); 

     // Register listener using the LocationRequest object 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 
      return; 
     } 
     LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this); 
    } 

    // Disconnect from Google Play Services when the Activity stops 
    @Override 
    protected void onStop() { 

     if (googleApiClient.isConnected()) { 
      LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this); 
      googleApiClient.disconnect(); 
     } 
     super.onStop(); 
     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     Action viewAction = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "MainWear Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       // make sure this auto-generated web page URL is correct. 
       // Otherwise, set the URL to null. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app URL is correct. 
       Uri.parse("android-app://com.example.arturo.pingutest/http/host/path") 
     ); 
     AppIndex.AppIndexApi.end(client, viewAction); 
     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client.disconnect(); 
    } 

    // Placeholders for required connection callbacks 
    @Override 
    public void onConnectionSuspended(int cause) { 

    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 

    } 

    @Override 
    public void onLocationChanged(Location location) { 

     // Display the latitude and longitude in the UI 
     Log.d("PINGU", "Latitude: " + String.valueOf(location.getLatitude()) + 
       "\nLongitude: " + String.valueOf(location.getLongitude())); 

     Lat=String.valueOf(location.getLatitude()); 
     Lon=String.valueOf(location.getLongitude()); 
    } 

} 
+0

你有AndroidManifest.xml中的<使用權限android:name =「android.permission.ACCESS_FINE_LOCATION」/>「嗎? – kazhik

回答

0

確保在您的應用清單擁有這些權限:

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

並添加以下代碼觸發請求權限活動:

ActivityCompat.requestPermissions(this, 
         new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},10); 

上面的代碼應該更換此評論在你的代碼

  // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details.