我是游戏开发者,不擅长android。我想创建一个“扩展服务”的库类,它发送GPS位置。
虽然我在onRequestPermissionsResult
导出的谷歌项目中保留了"MainActivity"
,但我没有遇到任何问题。但是,当我尝试将它放在我的库类中时,我开始收到以下错误。
@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;
} }
您不能在非活动类中覆盖onRequestPermissionsResult
,在您的情况下是Service
。用简单的话来说,onRequestPermissionsResult
是Activity
类中的方法,因此只能在扩展Activity
类的类中使用(你也可以使用AppCompatActivity
来兼容)。
限制也意味着ActivityCompat.requestPermissions
。第一个参数本身是Activity
类型而不是Context
.Passing Activity
obj直接,对于requestPermissions,在Service
不是一个好主意和不好的做法,因为它会导致内存泄漏和调试困境。
在Activity中移动您的权限请求和响应处理代码,并在活动中请求权限。