2016-10-26 7 views
0

ユーザーの場所を取得するためにgoogleapiクライアントを使用していますが、それはmarshmallowデバイスの下でうまくいきますが、marshmallowデバイスのアプリケーションがクラッシュしています。理由を知らない人が私を助けてくれます。Google ApiClientはマシュマロ以上の端末では機能していませんか?

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentSender; 
import android.location.Location; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GoogleApiAvailability; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.common.api.PendingResult; 
import com.google.android.gms.common.api.ResultCallback; 
import com.google.android.gms.common.api.Status; 
import com.google.android.gms.gcm.GcmNetworkManager; 
import com.google.android.gms.gcm.PeriodicTask; 
import com.google.android.gms.location.LocationListener; 
import com.google.android.gms.location.LocationRequest; 
import com.google.android.gms.location.LocationServices; 
import com.google.android.gms.location.LocationSettingsRequest; 
import com.google.android.gms.location.LocationSettingsResult; 
import com.google.android.gms.location.LocationSettingsStates; 
import com.google.android.gms.location.LocationSettingsStatusCodes; 

import java.text.DateFormat; 
import java.util.Date; 

import static precisioninfomatics.backgroundgps.MyLocationService.TASK_GET_LOCATION_PERIODIC; 

public class GPS extends Activity implements 
     LocationListener, 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, GetMethod { 
    private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; 
    private static final String TAG = "LocationActivity"; 
    private static final long INTERVAL = 1000 * 10; 
    private static final long FASTEST_INTERVAL = 1000 * 5; 
    Button btnFusedLocation; 
    TextView tvLocation; 
    LocationRequest mLocationRequest; 
    GoogleApiClient mGoogleApiClient; 
    Location mCurrentLocation; 
    String mLastUpdateTime; 
    private AsyncTaskGet asyncTaskGet; 
    protected static final int REQUEST_CHECK_SETTINGS = 0x1; 

    protected void createLocationRequest() { 
     mLocationRequest = new LocationRequest(); 
     mLocationRequest.setInterval(INTERVAL); 
     mLocationRequest.setFastestInterval(FASTEST_INTERVAL); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Log.d(TAG, "onCreate ..............................."); 
     if (!checkPlayServices()) { 
      finish(); 
     } 
     createLocationRequest(); 
     startService(new Intent(this, GPSService.class)); 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addApi(LocationServices.API) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .build(); 
     setContentView(R.layout.activity_gps); 
     tvLocation = (TextView) findViewById(R.id.tvLocation); 
     btnFusedLocation = (Button) findViewById(R.id.btnShowLocation); 
     btnFusedLocation.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       if (mCurrentLocation != null) { 
        String lat = String.valueOf(mCurrentLocation.getLatitude()); 
        String lng = String.valueOf(mCurrentLocation.getLongitude()); 
        String userID = "1"; 
        String time = String.valueOf(System.currentTimeMillis()); 
        String url = "http://172.16.6.106:8080/gpstracker/api/coordinates/" + lat + "/" + lng + "/" + userID + "/" + time; 
        Log.d("url", url); 
        GetNoteList(getApplicationContext()); 
        asyncTaskGet.execute(url); 

       } 
      } 
     }); 
     LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() 
       .addLocationRequest(mLocationRequest); 
     builder.setAlwaysShow(true); 
     PendingResult<LocationSettingsResult> result = 
       LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, 
         builder.build()); 
     result.setResultCallback(new ResultCallback<LocationSettingsResult>() { 
      @Override 
      public void onResult(LocationSettingsResult result) { 
       final Status status = result.getStatus(); 
       final LocationSettingsStates state = result.getLocationSettingsStates(); 
       switch (status.getStatusCode()) { 
        case LocationSettingsStatusCodes.SUCCESS: 
         startLocationUpdates(); 
         break; 
        case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: 
         // Location settings are not satisfied. But could be fixed by showing the user 
         // a dialog. 
         try { 
          // Show the dialog by calling startResolutionForResult(), 
          // and check the result in onActivityResult(). 
          status.startResolutionForResult(
            GPS.this, REQUEST_CHECK_SETTINGS); 
         } catch (IntentSender.SendIntentException e) { 
          // Ignore the error. 
         } 
         break; 
        case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: 
         // Location settings are not satisfied. However, we have no way to fix the 
         // settings so we won't show the dialog. 
         break; 
       } 
      } 
     }); 
     startPeriodicLocationTask(); 

    } 

    public void startPeriodicLocationTask() { 
     Log.d("periodictask", "startPeriodicLocationTask"); 
     GcmNetworkManager mGcmNetworkManager = GcmNetworkManager.getInstance(this); 
     PeriodicTask taskBuilder = new PeriodicTask.Builder() 
       .setService(MyLocationService.class) 
       .setTag(TASK_GET_LOCATION_PERIODIC) 
       .setPeriod(30).setFlex(20) 
       .setPersisted(true).build(); 
     mGcmNetworkManager.schedule(taskBuilder); 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
     Log.d(TAG, "onStart fired .............."); 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 
     Log.d(TAG, "onStop fired .............."); 
     mGoogleApiClient.disconnect(); 
     Log.d(TAG, "isConnected ...............: " + mGoogleApiClient.isConnected()); 
    } 

    private boolean checkPlayServices() { 
     GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance(); 
     int result = googleAPI.isGooglePlayServicesAvailable(this); 
     if(result != ConnectionResult.SUCCESS) { 
      if(googleAPI.isUserResolvableError(result)) { 
       googleAPI.getErrorDialog(this, result, 
         PLAY_SERVICES_RESOLUTION_REQUEST).show(); 
      } 

      return false; 
     } 

     return true; 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 
     Log.d(TAG, "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected()); 
     startLocationUpdates(); 
    } 

    protected void startLocationUpdates() { 
     PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
       mGoogleApiClient, mLocationRequest, this); 

     Log.d(TAG, "Location update started ..............: "); 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 

    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     Log.d(TAG, "Connection failed: " + connectionResult.toString()); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     Log.d(TAG, "Firing onLocationChanged.............................................."); 
     mCurrentLocation = location; 
     mLastUpdateTime = DateFormat.getTimeInstance().format(new Date()); 
     updateUI(); 
    } 

    private void updateUI() { 
     Log.d(TAG, "UI update initiated ............."); 
     if (null != mCurrentLocation) { 
      String lat = String.valueOf(mCurrentLocation.getLatitude()); 
      String lng = String.valueOf(mCurrentLocation.getLongitude()); 
      tvLocation.setText("At Time: " + mLastUpdateTime + "\n" + 
        "Latitude: " + lat + "\n" + 
        "Longitude: " + lng + "\n" + 
        "Accuracy: " + mCurrentLocation.getAccuracy() + "\n" + 
        "Provider: " + mCurrentLocation.getProvider()); 
     } else { 
      Log.d(TAG, "location is null ..............."); 
     } 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     stopLocationUpdates(); 
    } 

    protected void stopLocationUpdates() { 
     LocationServices.FusedLocationApi.removeLocationUpdates(
       mGoogleApiClient, this); 
     Log.d(TAG, "Location update stopped ......................."); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     if (mGoogleApiClient.isConnected()) { 
      startLocationUpdates(); 
      Log.d(TAG, "Location update resumed ....................."); 
     } 
    } 

    public void GetNoteList(Context context) { 
     asyncTaskGet = new AsyncTaskGet(context); 
     asyncTaskGet.getMethod = this; 
    } 

    @Override 
    public Void getDataFromServer(String objects) { 
     Log.d("response", objects); 
     return null; 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     switch (requestCode) { 
// Check for the integer request code originally supplied to startResolutionForResult(). 
      case REQUEST_CHECK_SETTINGS: 
       switch (resultCode) { 
        case Activity.RESULT_OK: 
         startLocationUpdates(); 
         break; 
        case Activity.RESULT_CANCELED: 
         Log.d("nogps", "nogps"); 
         break; 
       } 
       break; 
     } 
    } 
} 

私のGradle:

dependencies { 

    compile 'com.google.android.gms:play-services:9.8.0' 

    testCompile 'junit:junit:4.12' 
} 

私は後半googleplayサービスのバージョンを使用していますgoogleplayのサービスでミスを作っていますが、誰かがこの問題を解決するために私を助けることができると思います!

+1

マシュマロ以上のデバイスの**実行時アクセス許可**について聞きましたか? :) – SripadRaj

+0

マシュマロOSバージョン。ユーザーに場所の許可を求める必要があります。あなたのコードでは、許可のためのコードを追加しませんでした。クラッシュの原因かもしれません。また、クラッシュログを提供してください。 –

+0

私は実際のデバイスでテストしたので、クラッシュレポートはgeterrorダイアログ上に表示されることはありません –

答えて

1

Android M以上では、実行時のアクセス許可が問題になります。必要に応じて(iOSのように)アクセス許可をリクエストする必要が追加されました。

このリンクを参照してください:

// Here, thisActivity is the current activity 
if (ContextCompat.checkSelfPermission(thisActivity, 
      Manifest.permission.READ_CONTACTS) 
    != PackageManager.PERMISSION_GRANTED) { 

// Should we show an explanation? 
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, 
     Manifest.permission.READ_CONTACTS)) { 

    // Show an expanation to the user *asynchronously* -- don't block 
    // this thread waiting for the user's response! After the user 
    // sees the explanation, try again to request the permission. 

} else { 

    // No explanation needed, we can request the permission. 

    ActivityCompat.requestPermissions(thisActivity, 
      new String[]{Manifest.permission.READ_CONTACTS}, 
      MY_PERMISSIONS_REQUEST_READ_CONTACTS); 

    // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 
    // app-defined int constant. The callback method gets the 
    // result of the request. 
} 
} 
https://developer.android.com/training/permissions/requesting.html

多くのラッパーはここhttps://android-arsenal.com/tag/235?category=1

のようなサイトに簡単にアクセス権を追加するために周りにありますが沿ってあなたを助けるためにAndroidの開発者サイトから取られた、いくつかのコードです

関連する問題