无法解析“ onRequestPermissionsResult”

时间:2018-06-26 07:27:51

标签: java android service request gps

我是游戏开发者,不擅长android。我想制作一个“扩展服务”的库类,该类发送GPS位置。

虽然我将onRequestPermissionsResult保留在"MainActivity"的导出的google项目中,但没有遇到任何问题。但是,当我尝试将其放入库类时,开始出现以下错误。

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
    {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(grantResults.length>0&&grantResults[0]== PackageManager.PERMISSION_GRANTED)
        {
            if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED)
            {
                mLocationManager.requestLocationUpdates(mLocationManager.GPS_PROVIDER, 0, 0, locationListener);
            }
        }
    }

无法解析"onRequestPermissionsResult"


//------------------------------------------------------------

    package hp.test.com.mylibrary;


    import android.Manifest;
    import android.app.Activity;    
    import android.app.Service; 
    import android.content.Context;       
    import android.content.Intent;
    import android.content.pm.PackageManager;  
    import android.location.Location; 
    import android.location.LocationListener; 
    import android.location.LocationManager;  
    import android.os.Build;  
    import android.os.Bundle; 
    import android.os.IBinder;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.ActivityCompat; 
    import android.support.v4.content.ContextCompat; 

    public class GpsLocation  extends Service {
        public static GpsLocation Instance;                       
        public  LocationManager   mLocationManager;                         
        public  LocationListener  locationListener;                        
        public  Context mainContext;                                             
        public  Activity mainActivity;                                                        
        public  String[] premition={Manifest.permission.ACCESS_FINE_LOCATION};                 


        public static GpsLocation Instance ()                 
        {                                    
            if(Instance==null)                                         
            {                                           
                Instance = new GpsLocation();                                
            }                                                       
            return Instance;                                          
        }                                              

        public void setMainContext(Context context)                           
        {
            System.out.println("********** setMainContext");
            mainContext=context;
        }
        public void setmainActivity(Activity activity)
        {
            mainActivity=activity;
            System.out.println("********** setmainActivity"+ mainActivity);
        }
        public   void OpenLocationSeating()
        {
            mainActivity.startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
        }







        public void CreateData()
        {
            mLocationManager = (LocationManager) mainContext.getSystemService(LOCATION_SERVICE);
            locationListener = new LocationListener()
            {
                @Override
                public void onLocationChanged(Location location) {
                    System.out.println("********** onLocationChanged"+location.toString());
                }

                @Override
                public void onStatusChanged(String s, int i, Bundle bundle) {
                    System.out.println("********** onStatusChanged");

                }

                @Override
                public void onProviderEnabled(String s) {
                    System.out.println("********** onProviderEnabled");

                }

                @Override
                public void onProviderDisabled(String s) {
                    System.out.println("********** onProviderDisabled");

                }
            };
            checkPermissions();
        }





        public   void checkPermissions()
        {
            if (Build.VERSION.SDK_INT >= 23)
            {
                if (ActivityCompat.checkSelfPermission(mainContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mainContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
                {
                    ActivityCompat.requestPermissions(mainActivity,premition,1);
                }else
                {
                    mLocationManager.requestLocationUpdates(mLocationManager.GPS_PROVIDER, 0, 0, locationListener);
                }
            }
            else {
            }
        }




        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
        {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            if(grantResults.length>0&&grantResults[0]== PackageManager.PERMISSION_GRANTED)
            {
                if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED)
                {
                    mLocationManager.requestLocationUpdates(mLocationManager.GPS_PROVIDER, 0, 0, locationListener);
                }
            }
        }

        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }  }

1 个答案:

答案 0 :(得分:0)

在非活动类中,您不能覆盖onRequestPermissionsResult,在您的情况下,它不能覆盖Service。简而言之,onRequestPermissionsResultActivity类中的方法,因此只能在扩展Activity类的类中使用(您也可以将AppCompatActivity用于兼容性目的)。

该限制还意味着ActivityCompat.requestPermissions。第一个参数本身的类型为Activity,而不是Context。在Activity中将Service obj直接传递给requestPermissions并不是一个好主意和坏习惯,因为它会导致内存泄漏和调试问题。

在“活动”中移动您的权限请求和响应处理代码,然后在活动中移动请求权限。