请帮助...我是Android开发的新手......我搜索了我的问题,但没有得到任何解决方案...... 我也尝试过: Didn't understand it
我已经获得了所有已安装应用的列表,所以现在我希望在点击文本视图时显示每个应用程序的所有权限
所有已安装应用的列表:
主要活动:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView userInstalledApps = (ListView)findViewById(R.id.installed_app_list);
List<AppList> installedApps = getInstalledApps();
AppAdapter installedAppAdapter = new AppAdapter(MainActivity.this, installedApps);
userInstalledApps.setAdapter(installedAppAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private List<AppList> getInstalledApps() {
List<AppList> res = new ArrayList<AppList>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
if ((isSystemPackage(p) == false)) {
String appName = p.applicationInfo.loadLabel(getPackageManager()).toString();
Drawable icon = p.applicationInfo.loadIcon(getPackageManager());
res.add(new AppList(appName, icon));
}
}
return res;
}
private boolean isSystemPackage(PackageInfo pkgInfo) {
return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true : false;
}
}
AppList类:
public class AppList {
private String name;
Drawable icon;
public AppList(String name, Drawable icon) {
this.name = name;
this.icon = icon;
}
public String getName() {
return name;
}
public Drawable getIcon() {
return icon;
}
}
AppAdapter:
public class AppAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private List<AppList> listStorage;
public AppAdapter(Context context, List<AppList> customizedListView) {
layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listStorage = customizedListView;
}
@Override
public int getCount() {
return listStorage.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder listViewHolder;
if(convertView == null){
listViewHolder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.installed_app_list, parent, false);
listViewHolder.textInListView = (TextView)convertView.findViewById(R.id.list_app_name);
listViewHolder.imageInListView = (ImageView)convertView.findViewById(R.id.app_icon);
convertView.setTag(listViewHolder);
}else{
listViewHolder = (ViewHolder)convertView.getTag();
}
listViewHolder.textInListView.setText(listStorage.get(position).getName());
listViewHolder.imageInListView.setImageDrawable(listStorage.get(position).getIcon());
return convertView;
}
static class ViewHolder{
TextView textInListView;
ImageView imageInListView;
}
}
activity_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"
android:background="#eceaea">
<ListView
android:id="@+id/installed_app_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:scrollbars="none" />
</RelativeLayout>
installed_apps_list.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/app_icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:contentDescription="@string/hello_world"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:paddingBottom="8dp"/>
<TextView
android:id="@+id/list_app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16dp"
android:textColor="#000000"
android:layout_toRightOf="@+id/app_icon"
android:layout_alignTop="@+id/app_icon"
android:layout_marginLeft="16dp"
android:layout_marginTop="2dp"
android:text="@string/hello_world"/>
</RelativeLayout>
这就是我所做的......我的意思是我现在已经安装了所有已安装的应用程序 在点击任何列出的应用程序时想要每个应用程序权限....
答案 0 :(得分:0)
您还需要检索PackageName
以及Permissions
,如下所示。
PackageManager pm = getPackageManager();
List<ApplicationInfo> packages =pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo applicationInfo : packages)
{
Log.d("test", "App: " + applicationInfo.name + " Package: " + applicationInfo.packageName);
try
{
PackageInfo packageInfo = pm.getPackageInfo(applicationInfo.packageName, PackageManager.GET_PERMISSIONS);
//Get Permissions
String[] requestedPermissions = packageInfo.requestedPermissions;
if(requestedPermissions != null)
{
for (int i = 0; i < requestedPermissions.length; i++)
{
Log.d("test", requestedPermissions[i]);
}
}
}
catch (NameNotFoundException e)
{
e.printStackTrace();
}
}
在您的模态类field
中添加AppList
权限并填充它。
在listview userInstalledApps
上添加侦听器
userInstalledApps.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
AppList appList = installedAppAdapter.getItem(position);
String permissions = appList.getPermission();
//Do whatever you want with permissions here.
}
});
请通过此段代码在AppAdapter
课程中进行更正
@Override
public AppList getItem(int position) {
return listStorage.get(position);
}
根据您的需要使用以下代码,请使用此
替换相应的代码private List<AppList> getInstalledApps()
{
List<AppList> res = new ArrayList<AppList>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
if ((isSystemPackage(p) == false))
{
String appName = p.applicationInfo.loadLabel(getPackageManager()).toString();
Drawable icon = p.applicationInfo.loadIcon(getPackageManager());
try {
PackageInfo packageInfo = getPackageManager().getPackageInfo(p.applicationInfo.packageName, PackageManager.GET_PERMISSIONS);
//Get Permissions
String[] requestedPermissions = packageInfo.requestedPermissions;
StringBuilder sbPermissionSt = new StringBuilder();
if(requestedPermissions != null)
{
for (int j = 0; j < requestedPermissions.length; j++)
{
Log.d("test", requestedPermissions[j]);
sbPermissionSt.append(requestedPermissions[j]+",");
}
}
res.add(new AppList(appName, icon, sbPermissionSt.toString()));
}
catch (PackageManager.NameNotFoundException nme)
{
nme.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
return res;
}
您的AppList.java
public class AppList
{
private String name;
Drawable icon;
private String permissions;
public AppList(String name, Drawable icon, String permissions)
{
this.name = name;
this.icon = icon;
this.permissions = permissions;
}
public String getName() {
return name;
}
public Drawable getIcon() {
return icon;
}
public String getPermissions() {
return permissions;
}
public void setPermissions(String permissions) {
this.permissions = permissions;
}
}
在将适配器设置为listview(附加侦听器)
之后添加此代码userInstalledApps.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
AppList appList = installedAppAdapter.getItem(position);
String permissions = appList.getPermissions();
Toast.makeText(MainActivity.this, permissions, Toast.LENGTH_LONG).show();
}
});