import java.util.List;
import android.support.v7.app.ActionBarActivity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListneronButton();
}
public void addListneronButton(){
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
StringBuffer appNameAndPermissions=new StringBuffer();
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);
appNameAndPermissions.append(packageInfo.packageName+"*:\n");
//Get Permissions
String[] requestedPermissions = packageInfo.requestedPermissions;
if(requestedPermissions != null) {
for (int i = 0; i < requestedPermissions.length; i++) {
Log.d("test", requestedPermissions[i]);
appNameAndPermissions.append(requestedPermissions[i]+"\n");
}
appNameAndPermissions.append("\n");
}
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
}
});
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.amazon.permissiondetector.MainActivity"
tools:ignore="MergeRootFrame"
android:background="@color/black">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="170dp"
android:height="60dp"
android:text="@string/buttontext"
android:width="150dp" />
</RelativeLayout>
我是Android开发的新手,所以没有多少想法。创建应用程序以检测连接设备中所有应用程序使用的权限。就像插件探测器一样。我应该像App tile一样显示app.Guys使用的权限帮助我!!
提前致谢!
答案 0 :(得分:1)
您需要在布局文件中添加TextView小部件。例如,您可以在按钮下方添加:
<TextView
android:id="@+id/txt_permissions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
然后在addListneronButton()
末尾设置它的文字属性,如下所示:
txt_permissions.setText(appNameAndPermissions.toString())