2016-09-23 73 views
3

我有一个WebView和某个页面加载需要GeolocationPermissions权限。为此我重写GeolocationPermissionsShowPrompt()。在运行时请求权限位置Android Marshmallow 6.0 webview

此外,我的应用程序针对SDK 23(Android M)与新的权限模型。我使用此代码但无法工作,无法显示对话框,无法确定当前位置,有人可以帮助我吗?

感谢

private static final int REQUEST_INTERNET = 200; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    ActionBar actionBar = getSupportActionBar(); 
    if (null != actionBar) { 
     actionBar.hide(); 
    } 
    if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){ 
     ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_INTERNET); 
    } 

    String url = "https://google.com"; 
    WebView view = (WebView) this.findViewById(R.id.WebView); 
    view.setWebViewClient(new MyBrowser()); 
    view.getSettings().setJavaScriptEnabled(true); 
    view.getSettings().setDomStorageEnabled(true); 
    view.getSettings().setAppCacheEnabled(true); 
    view.getSettings().setDatabaseEnabled(true); 
    view.getSettings().setGeolocationEnabled(true); 
    view.getSettings().setGeolocationEnabled(true); 
    view.setWebChromeClient(new GeoWebChromeClient()); 
    view.loadUrl(url); 
} 

询问请求

@Override 
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
    if (grantResults[0]== PackageManager.PERMISSION_GRANTED){ 

    } else if(grantResults[0] == PackageManager.PERMISSION_DENIED){ 
     if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)){ 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setMessage("This permission is important to Access Location.") 
        .setTitle("Important permission required"); 

      builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_INTERNET); 
       } 
     }); 
      ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_INTERNET); 
    } else { 
     } 
    } 
} 

public class GeoWebChromeClient extends WebChromeClient { 
    @Override 
    public void onGeolocationPermissionsShowPrompt(String origin GeolocationPermissions.Callback callback) { 
        callback.invoke(origin, true, false); 
    } 
} 

回答

0

我已经在GitHub上的Android库位置经理,别人说不定哪天有用的解决了这个问题: )

LocationManager

相关问题