-2

我的应用程序被强制关闭。请帮我。我想获取我的手机中的应用程序正在使用的权限列表。

package com.permissions;

import android.os.Bundle;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.view.Menu;
import android.widget.TextView;

public class GetPermissions extends Activity {

TextView tvShow;

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

    tvShow = (TextView) findViewById(R.id.tvshow);
    tvShow.setText(PackageManager.GET_PERMISSIONS);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.get_permissions, menu);
    return true;
}

}
4

1 回答 1

1

首先,这条线tvShow.setText(PackageManager.GET_PERMISSIONS);对你没有帮助。它只会让您获得您在清单文件中提供的权限。未在清单文件中声明的所需权限将显示在 LogCat 错误中。

如果您想获得在您的应用程序中使用的权限,那么此处的代码将有所帮助

final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = getPackageManager().queryIntentActivities(mainIntent, 0);

for (Object obj : pkgAppsList) {
  ResolveInfo resolveInfo = (ResolveInfo) obj;
  PackageInfo packageInfo = null;
  try {
    packageInfo = getPackageManager().getPackageInfo(resolveInfo.activityInfo.packageName, PackageManager.GET_PERMISSIONS);
  } catch (NameNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }

  String[] requestedPermissions = packageInfo.requestedPermissions;
}
于 2013-08-13T13:51:21.360 回答