萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> 查看Android應用所需權限

查看Android應用所需權限

   MainActivity如下:

  package cc.testusespermission;

  import android.app.Activity;

  import android.content.pm.PackageInfo;

  import android.content.pm.PackageManager;

  import android.content.pm.PermissionGroupInfo;

  import android.content.pm.PermissionInfo;

  import android.os.Bundle;

  /**

  * Demo描述:

  * 查看Android應用所需權限(uses-permission)

  *

  * 參考資料:

  * 1 http://blog.csdn.net/bage1988320/article/details/6740292

  * 2 http://blog.csdn.net/hjd_love_zzt/article/details/12238301

  * Thank you very much

  *

  * 備注說明:

  * 在Demo中的代碼注釋是以android.permission.INTERNET權限

  * 為例子對各屬性進行了詳細說明

  *

  */

  public class MainActivity extends Activity {

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  getUsesPermission("cc.testusespermission");

  }

  private void getUsesPermission(String packageName){

  try {

  PackageManager packageManager=this.getPackageManager();

  PackageInfo packageInfo=packageManager.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);

  String [] usesPermissionsArray=packageInfo.requestedPermissions;

  for (int i = 0; i < usesPermissionsArray.length; i++) {

  //得到每個權限的名字,如:android.permission.INTERNET

  String usesPermissionName=usesPermissionsArray[i];

  System.out.println("usesPermissionName="+usesPermissionName);

  //通過usesPermissionName獲取該權限的詳細信息

  PermissionInfo permissionInfo=packageManager.getPermissionInfo(usesPermissionName, 0);

  //獲得該權限屬於哪個權限組,如:網絡通信

  PermissionGroupInfo permissionGroupInfo = packageManager.getPermissionGroupInfo(permissionInfo.group, 0);

  System.out.println("permissionGroup=" + permissionGroupInfo.loadLabel(packageManager).toString());

  //獲取該權限的標簽信息,比如:完全的網絡訪問權限

  String permissionLabel=permissionInfo.loadLabel(packageManager).toString();

  System.out.println("permissionLabel="+permissionLabel);

  //獲取該權限的詳細描述信息,比如:允許該應用創建網絡套接字和使用自定義網絡協議

  //浏覽器和其他某些應用提供了向互聯網發送數據的途徑,因此應用無需該權限即可向互聯網發送數據.

  String permissionDescription=permissionInfo.loadDescription(packageManager).toString();

  System.out.println("permissionDescription="+permissionDescription);

  System.out.println("===========================================");

  }

  } catch (Exception e) {

  // TODO: handle exception

  }

  }

  }

main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查看Android應用所需權限(uses-permission)" 
        android:layout_centerInParent="true"
    />

</RelativeLayout>

copyright © 萬盛學電腦網 all rights reserved