由于缺少位置许可,应用程序在启动时崩溃

时间:2018-04-04 23:29:51

标签: java android android-permissions android-location

由于缺少权限,我的应用程序在启动时崩溃。

我已经在manifest.xml中添加了这些缺少的权限,并在类中实现了运行时权限。

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

这是我的logcat。当我上次打开它时它才起作用,我没有改变任何东西。

可能是什么问题?

E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.example.escaper.smoosh4340, PID: 29873
   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.escaper.smoosh4340/com.example.escaper.smoosh4340.LocationWeather}: java.lang.SecurityException: "network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
       at android.app.ActivityThread.-wrap12(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:154)
       at android.app.ActivityThread.main(ActivityThread.java:6119)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
    Caused by: java.lang.SecurityException: "network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.
       at android.os.Parcel.readException(Parcel.java:1684)
       at android.os.Parcel.readException(Parcel.java:1637)
       at android.location.ILocationManager$Stub$Proxy.getLastLocation(ILocationManager.java:725)
       at android.location.LocationManager.getLastKnownLocation(LocationManager.java:1205)
       at com.example.escaper.smoosh4340.LocationWeather.onCreate(LocationWeather.java:64)
       at android.app.Activity.performCreate(Activity.java:6679)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)

这是我的java类

  private static final String APP_ID="1c163fcac8ac8ca256fd3119777bee59";
    Button button;
    TextView textView;
    public static final int MY_PERMISSION_REQUEST_LOCATION = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_weather);

        button = (Button)findViewById(R.id.btnGetlc);

        textView = (TextView)findViewById(R.id.textlc);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ContextCompat.checkSelfPermission(LocationWeather.this,
                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
                    if(ActivityCompat.shouldShowRequestPermissionRationale(LocationWeather.this,
                            Manifest.permission.ACCESS_COARSE_LOCATION)){
                        ActivityCompat.requestPermissions(LocationWeather.this,
                                new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSION_REQUEST_LOCATION);

                    }else {
                        ActivityCompat.requestPermissions(LocationWeather.this,
                                new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSION_REQUEST_LOCATION);
                    }
                }else {
                    LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                    Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    try{
                        textView.setText(yourLocation(location.getLatitude(), location.getLongitude()));
                    }catch (Exception e){
                        e.printStackTrace();
                        Toast.makeText(LocationWeather.this, "NOT FOUND, TURN ON GPS", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);









        String units = "metric";
        String url = String.format("http://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&units=%s&appid=%s",
                location.getLatitude(), location.getLongitude(), units,APP_ID);

        TextView textView = (TextView) findViewById(R.id.textweat);
        new WeatherDetection(textView).execute(url);

    }

    @Override
    public void onRequestPermissionsResult(int requestCode,  String[] permissions,  int[] grantResults) {
        switch (requestCode){
            case MY_PERMISSION_REQUEST_LOCATION:{
                if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    if(ContextCompat.checkSelfPermission(LocationWeather.this,
                            Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED){
                        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        try{
                            textView.setText(yourLocation(location.getLatitude(), location.getLongitude()));
                        }catch (Exception e){
                            e.printStackTrace();
                            Toast.makeText(LocationWeather.this, "NOT FOUND, TURN ON GPS", Toast.LENGTH_SHORT).show();
                        }
                    }
                }else{
                    Toast.makeText(LocationWeather.this, "No permission!", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

查看您的例外

<强> Caused by: java.lang.SecurityException: "network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.

它说你错过了

的许可

android.permission.ACCESS_FINE_LOCATION

将该权限添加到menifest.xml以及您的类代码。

答案 1 :(得分:0)

在致电之前,您没有检查自己的权限:

locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

您只需在点击按钮后检查权限,这样就太晚了。