タイトルが示すように、私はonRequestPermissionsResult
抽象メソッドをオーバーライドしようとしていますが、Gradleはメソッドのスーパークラスがない。superClassの下で抽象メソッドであるにもかかわらず、onRequestPermissionsResult関数をオーバーライドできませんでした。android.support.v4.app.ActivityCompat
the documentationによれば、スーパークラスはActivityCompat
です。
私は場所ベースのアプリケーションのための場所オブジェクトを作成しています。
この問題を解決するにはどうすればよいですか?それがオーバーライドするonRequestPermissionsResult
持っていないので、
import android.*;
import android.Manifest;
import android.content.IntentSender;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.app.Activity;
import android.app.PendingIntent;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingApi;
import com.google.android.gms.location.GeofencingRequest;
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.LocationSettingsResult;
import android.content.ContextWrapper;
import android.content.Context;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v4.content.ContextCompat;
import android.util.Log;
/**
* Created by User on 7/2/2016.
*/
public class GoogleLocationHandler implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener{
public GoogleApiClient mGoogleApiClient;
public static final String TAG = GoogleLocationHandler.class.getSimpleName();
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private final static int MY_PERMISSION_ACCESS_FINE_LOCATION = 1;
private static Context parentBase;
private static Activity parentActivity;
private static PendingIntent parentIntent;
public Location phoneLocation;
private LocationRequest requestLocation;
public GoogleLocationHandler(Context Base, Activity activityBase, PendingIntent intent){
parentBase = Base;
parentActivity = activityBase;
parentIntent = intent;
initialLocationService(parentBase);
}
public void initialLocationService(Context Base)
{
mGoogleApiClient = new GoogleApiClient.Builder(Base)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
requestLocation = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000) // 10 seconds, in milliseconds
.setFastestInterval(1 * 1000); // 1 second, in milliseconds
}
public void connect()
{
mGoogleApiClient.connect();
}
public void pause(){
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, parentIntent);
mGoogleApiClient.disconnect();
}
}
public void disconnect()
{
if (mGoogleApiClient.isConnected())
{
mGoogleApiClient.disconnect();
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
if(PackageManager.PERMISSION_GRANTED ==
(ContextCompat.checkSelfPermission(parentBase, Manifest.permission.ACCESS_FINE_LOCATION)))
{
phoneLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (phoneLocation == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, requestLocation, parentIntent);
} else {
Log.d(TAG, phoneLocation.toString());
}
}
else
{
ActivityCompat.requestPermissions(parentActivity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSION_ACCESS_FINE_LOCATION);
}
}
//public void onRequestPermissionsResult(int requestCode,
// String permissions[], int[] grantResults)
@Override
public void onRequestPermissionsResult(int requestCode,@NonNull String permissions[],
@NonNull int [] grantResults)
{
switch(requestCode)
{
case MY_PERMISSION_ACCESS_FINE_LOCATION:{
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if(PackageManager.PERMISSION_GRANTED ==
(ContextCompat.checkSelfPermission(parentBase, Manifest.permission.ACCESS_FINE_LOCATION)))
{
phoneLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (phoneLocation == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, requestLocation, parentIntent);
} else {
Log.d(TAG, phoneLocation.toString());
}
}
else
{
ActivityCompat.requestPermissions(parentActivity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSION_ACCESS_FINE_LOCATION);
}
}
}
}
}
AHA。どうもありがとう。私はスーパークラスがインターフェイスだったことに気付かなかった少し不協和音を感じる。 – law