Android 6权限访问互联网

时间:2016-10-19 11:58:26

标签: android android-permissions

一个月前我写了我的第一个应用程序并且我允许在sd-card上写,当活动开始我请求权限时我做了:

if (permission != PackageManager.PERMISSION_GRANTED) {

    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("L'app richiede i permessi di scrittura")
                .setTitle("Richiesta Permessi");

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int id) {

                makeRequest();
            }
        });

        AlertDialog dialog = builder.create();
        dialog.show();

    } else {
        makeRequest();
    }
}

这是makeRequest()

private static final int REQUEST_WRITE_STORAGE = 112;
private static String TAG = "PermissionDemo";

protected void makeRequest() {
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
            REQUEST_WRITE_STORAGE);
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case REQUEST_WRITE_STORAGE: {
            if (grantResults.length == 0
                    || grantResults[0] !=
                    PackageManager.PERMISSION_GRANTED) {

               //Log.i(TAG, "Permission has been denied by user");

                finish();
            } else {
                //Log.i(TAG, "Permission has been granted by user");
                creaConfigurazioneIniziale();
            }
            return;
        }
    }
}

现在我的应用程序需要获得互联网许可才能将其放在清单上:

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

这个     

但不起作用,我可以做些什么来增加互联网和网络的权限? 我试着添加makereqeust这个:

protected void makeRequest() {
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
            REQUEST_WRITE_STORAGE);
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.INTERNET},
            REQUEST_WRITE_STORAGE);
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.ACCESS_NETWORK_STATE},
            REQUEST_WRITE_STORAGE);
}

但不起作用。我该如何解决问题?

2 个答案:

答案 0 :(得分:13)

您无需申请互联网或access_network_state权限,因为Android默认授予它。

我们必须检查一些危险的权限

危险许可列表

  

日历: READ_CALENDAR,WRITE_CALENDAR

     

CAMERA: CAMERA

     

联系人: READ_CONTACTS,WRITE_CONTACTS,GET_ACCOUNTS

     

位置:ACCESS_FINE_LOCATION,ACCESS_COARSE_LOCATION

     

MICROPHONE: RECORD_AUDIO

     

电话: READ_PHONE_STATE,CALL_PHONE,READ_CALL_LOG,WRITE_CALL_LOG,ADD_VOICEMAIL,USE_SIP,PROCESS_OUTGOING_CALLS

     

传感器: BODY_SENSORS

     

SMS: SEND_SMS,RECEIVE_SMS,READ_SMS,RECEIVE_WAP_PUSH,RECEIVE_MMS

     

存储: READ_EXTERNAL_STORAGE,WRITE_EXTERNAL_STORAGE

正常权限列表

  

ACCESS_LOCATION_EXTRA_COMMANDS

     

ACCESS_NETWORK_STATE

     

ACCESS_NOTIFICATION_POLICY

     

ACCESS_WIFI_STATE

     

BLUETOOTH

     

BLUETOOTH_ADMIN

     

BROADCAST_STICKY

     

CHANGE_NETWORK_STATE

     

CHANGE_WIFI_MULTICAST_STATE

     

CHANGE_WIFI_STATE

     

DISABLE_KEYGUARD

     

EXPAND_STATUS_BAR

     

手电筒

     

GET_PACKAGE_SIZE

     

INTERNET

     

KILL_BACKGROUND_PROCESSES

     

许可权

     

NFC

     

READ_SYNC_SETTINGS

     

READ_SYNC_STATS

     

项值

     

REORDER_TASKS

     

REQUEST_INSTALL_PACKAGES

     

SET_TIME_ZONE

     

SET_WALLPAPER

     

SET_WALLPAPER_HINTS

     

TRANSMIT_IR

     

USE_FINGERPRINT

     

VIBRATE

     

WAKE_LOCK

     

WRITE_SYNC_SETTINGS

     

SET_ALARM

     

INSTALL_SHORTCUT

     

UNINSTALL_SHORTCUT

有关详细信息,请查看此信息。 Normal and Dangerous Permissions

如何检查危险权限?

我按照我的方式发布代码。假设我们要检查CAMARA权限。

public class Utility {

    public static final int MY_PERMISSIONS_REQUEST_CAMERA = 130;

    public static boolean checkPermissionCAMERA(final Context context) {
        int currentAPIVersion = Build.VERSION.SDK_INT;
        if (currentAPIVersion >= android.os.Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(context,
                    Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context,
                        Manifest.permission.CAMERA)) {
                    AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
                    alertBuilder.setCancelable(true);
                    alertBuilder.setTitle("Permission necessary");
                    alertBuilder.setMessage("Camera permission is necessary");
                    alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            ActivityCompat.requestPermissions((Activity) context,
                                    new String[] { Manifest.permission.CAMERA }, MY_PERMISSIONS_REQUEST_CAMERA);
                        }
                    });
                    AlertDialog alert = alertBuilder.create();
                    alert.show();

                } else {
                    ActivityCompat.requestPermissions((Activity) context, new String[] { Manifest.permission.CAMERA },
                            MY_PERMISSIONS_REQUEST_CAMERA);
                }
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }

    public static void showDialogOK(final Activity context, String message) {
        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
        alertBuilder.setCancelable(true);
        alertBuilder.setTitle("Permission necessary");
        alertBuilder.setMessage(message);
        alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Utility.checkAndRequestPermissions(context);
            }
        });
        AlertDialog alert = alertBuilder.create();
        alert.show();
    }
}

现在进入您的活动。

if(Utility.checkPermissionCAMERA(this)){
    // Do work
}

并最终覆盖onRequestPermissionsResult

@Override
public void onRequestPermissionsResult(int requestCode,
        String[] permissions, int[] grantResults) {
    switch (requestCode) {
    case Utility.MY_PERMISSIONS_REQUEST_CAMERA:
        if (ContextCompat.checkSelfPermission(Splash_Activity.this,
                Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
            // do work
        }else{
            // take action
        } 
        break;
    }
}

编辑:

如何检查多个权限?

public class Utility {

    public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 101;

    public static boolean checkAndRequestPermissions(final Activity context) {
        int ExtstorePermission = ContextCompat.checkSelfPermission(context,
                Manifest.permission.READ_EXTERNAL_STORAGE);
        int WExtstorePermission = ContextCompat.checkSelfPermission(context,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
        int cameraPermission = ContextCompat.checkSelfPermission(context,
                Manifest.permission.CAMERA);
        int READ_PHONE_STATE = ContextCompat.checkSelfPermission(context,
                Manifest.permission.READ_PHONE_STATE);
        int location = ContextCompat.checkSelfPermission(context,
                Manifest.permission.ACCESS_FINE_LOCATION);
        int READ_CONTACTS = ContextCompat.checkSelfPermission(context,
                Manifest.permission.READ_CONTACTS);
        int RECORD_AUDIO = ContextCompat.checkSelfPermission(context,
                Manifest.permission.RECORD_AUDIO);
        int internet = ContextCompat.checkSelfPermission(context,
                Manifest.permission.INTERNET);
        List<String> listPermissionsNeeded = new ArrayList<>();
        if (cameraPermission != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.CAMERA);
        }
        if (WExtstorePermission != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded
                    .add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
        if (ExtstorePermission != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded
                    .add(Manifest.permission.READ_EXTERNAL_STORAGE);
        }
        if (location != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
        }
        if (READ_PHONE_STATE != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.READ_PHONE_STATE);
        }
        if (READ_CONTACTS != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.READ_CONTACTS);
        }
        if (RECORD_AUDIO != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.RECORD_AUDIO);
        }
        if (internet != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.INTERNET);
        }
        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(context, listPermissionsNeeded
                    .toArray(new String[listPermissionsNeeded.size()]),
                    REQUEST_ID_MULTIPLE_PERMISSIONS);
            return false;
        }
        return true;
    }

    public static void showDialogOK(final Activity context, String message) {
        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
        alertBuilder.setCancelable(true);
        alertBuilder.setTitle("Permission necessary");
        alertBuilder.setMessage(message);
        alertBuilder.setPositiveButton(android.R.string.yes,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Utility.checkAndRequestPermissions(context);
                    }
                });
        AlertDialog alert = alertBuilder.create();
        alert.show();
    }
}

我希望这会有所帮助

答案 1 :(得分:2)

请检查link1link2

对于将访问互联网的应用程序,它仍然是必需的。如果开发人员发布应用程序而未在Android清单中定义该应用程序,则在第一次进行连接尝试时将抛出异常,并且应用程序可能会崩溃。这和以前没什么不同。

安装时授予的有限权限:当用户安装或更新应用程序时,系统会向应用程序授予应用程序请求属于PROTECTION_NORMAL的所有权限。例如,闹钟和互联网权限属于PROTECTION_NORMAL,因此它们会在安装时自动授予。

由于