Android 获得一个应用的权限
2016-03-09 22:23
399 查看
今天也许是一个值得高兴的日子,但是我却不知道有什么值得高兴的,但是室友们却在打麻将庆祝,好吧。我这是找个借口表达室友们打麻将的不满,吐槽完毕,开始码字,首先,先链接一个网址http://blog.csdn.net/stonecao/article/details/6425019 这里讲的是关于aidl这个玩意的解释,请看官们耐心的去看看,看完之后,就能对aidl有个初步了解,从而开始正题
在开始前,还有一个小小的要求,放心,不是要鱼丸什么的,只是要求各位看官去看看关于packmanager的简单介绍,我就不累赘了,大家自己去百度百度吧。
接下来开始讲讲菜逼的我是怎么做的,首先,自己定义一个class,用来装要用的数据,我就直接上代码了
public class Appinfo implements Serializable{
private String packageName;//包名
private boolean isSystemApp;//是不是系统的app
private long cachesize;
private long appsize;
private long datasize;
private Drawable icon;//图片
private String appName;//显示的名字,比如微信
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public boolean isSystemApp() {
return isSystemApp;
}
public void setSystemApp(boolean isSystemApp) {
this.isSystemApp = isSystemApp;
}
public long getCachesize() {
return cachesize;
}
public void setCachesize(long cachesize) {
this.cachesize = cachesize;
}
public long getAppsize() {
return appsize;
}
public void setAppsize(long appsize) {
this.appsize = appsize;
}
public long getDatasize() {
return datasize;
}
public void setDatasize(long datasize) {
this.datasize = datasize;
}
}
在这里,大家可以不用实现序列化这个接口的,我是因为有自己用途,在我定义的东西里,醉重要的是一个packname;顾名思义这是包名;如“com.example.a”这种格式的字符串;按照我的做法,用packmanager后得到了packname后。开始正题了
先上一段百度找到的代码:
Class clazz = getClass().getClassLoader().loadClass("android.widget.AppSecurityPermissions");
Constructor constructor = clazz.getConstructor(new Class[] { Context.class, String.class });
Object object = constructor.newInstance(new Object[] { this, name });
Method method = clazz.getDeclaredMethod("getPermissionsView", new Class[] {});
View view = (View) method.invoke(object, new Object[] {});
这段代码看下就可以知道是返回一个view的,所以最后得靠自己把它加到布局文件里面,这段代码的作用,是通过
name这个包名以及java的反射机制来获得视图(对于java的反射机制,欢迎大家去了解了解,因为android有时也会用到反射机制),所以不用改太多的代码,只需要改成自己的包名,还有加上try--catch;在这里推荐各位使用代码动态的控制布局,这点还是上点代码吧
public class ShowApp extends Activity {
android.widget.LinearLayout.LayoutParams layoutParams1;
String name;
long datasize;
long cachesize;
GetPermission getpermission;
View v;
LinearLayout relativelayout;
TextView textview1, textview2, textview3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
layoutParams1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
relativelayout = new LinearLayout(this);
relativelayout.setOrientation(LinearLayout.VERTICAL);
relativelayout.setGravity(Gravity.VERTICAL_GRAVITY_MASK);
textview1 = new TextView(this);
textview2 = new TextView(this);
textview3 = new TextView(this);
name = getIntent().getStringExtra("list");
cachesize = getIntent().getLongExtra("cachesize", 0);
datasize = getIntent().getLongExtra("datasize", 0);
textview1.setText("" + cachesize);
textview2.setText("" + datasize);
Class clazz = getClass().getClassLoader().loadClass("android.widget.AppSecurityPermissions");
Constructor constructor = clazz.getConstructor(new Class[] { Context.class, String.class });
Object object = constructor.newInstance(new Object[] { this, name });
Method method = clazz.getDeclaredMethod("getPermissionsView", new Class[] {});
View view = (View) method.invoke(object, new Object[] {});
relativelayout.addView(textview1, layoutParams1);
relativelayout.addView(textview2, layoutParams1);
relativelayout.addView(view);
setContentView(relativelayout);
} catch (Exception e) {
e.printStackTrace();
}
}
}
在这里基本告诫,最后上上效果图,写的有点啰嗦,是为了照顾一些跟我一样的菜鸟,希望大家不要喷,谢谢
在开始前,还有一个小小的要求,放心,不是要鱼丸什么的,只是要求各位看官去看看关于packmanager的简单介绍,我就不累赘了,大家自己去百度百度吧。
接下来开始讲讲菜逼的我是怎么做的,首先,自己定义一个class,用来装要用的数据,我就直接上代码了
public class Appinfo implements Serializable{
private String packageName;//包名
private boolean isSystemApp;//是不是系统的app
private long cachesize;
private long appsize;
private long datasize;
private Drawable icon;//图片
private String appName;//显示的名字,比如微信
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public boolean isSystemApp() {
return isSystemApp;
}
public void setSystemApp(boolean isSystemApp) {
this.isSystemApp = isSystemApp;
}
public long getCachesize() {
return cachesize;
}
public void setCachesize(long cachesize) {
this.cachesize = cachesize;
}
public long getAppsize() {
return appsize;
}
public void setAppsize(long appsize) {
this.appsize = appsize;
}
public long getDatasize() {
return datasize;
}
public void setDatasize(long datasize) {
this.datasize = datasize;
}
}
在这里,大家可以不用实现序列化这个接口的,我是因为有自己用途,在我定义的东西里,醉重要的是一个packname;顾名思义这是包名;如“com.example.a”这种格式的字符串;按照我的做法,用packmanager后得到了packname后。开始正题了
先上一段百度找到的代码:
Class clazz = getClass().getClassLoader().loadClass("android.widget.AppSecurityPermissions");
Constructor constructor = clazz.getConstructor(new Class[] { Context.class, String.class });
Object object = constructor.newInstance(new Object[] { this, name });
Method method = clazz.getDeclaredMethod("getPermissionsView", new Class[] {});
View view = (View) method.invoke(object, new Object[] {});
这段代码看下就可以知道是返回一个view的,所以最后得靠自己把它加到布局文件里面,这段代码的作用,是通过
name这个包名以及java的反射机制来获得视图(对于java的反射机制,欢迎大家去了解了解,因为android有时也会用到反射机制),所以不用改太多的代码,只需要改成自己的包名,还有加上try--catch;在这里推荐各位使用代码动态的控制布局,这点还是上点代码吧
public class ShowApp extends Activity {
android.widget.LinearLayout.LayoutParams layoutParams1;
String name;
long datasize;
long cachesize;
GetPermission getpermission;
View v;
LinearLayout relativelayout;
TextView textview1, textview2, textview3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
layoutParams1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
relativelayout = new LinearLayout(this);
relativelayout.setOrientation(LinearLayout.VERTICAL);
relativelayout.setGravity(Gravity.VERTICAL_GRAVITY_MASK);
textview1 = new TextView(this);
textview2 = new TextView(this);
textview3 = new TextView(this);
name = getIntent().getStringExtra("list");
cachesize = getIntent().getLongExtra("cachesize", 0);
datasize = getIntent().getLongExtra("datasize", 0);
textview1.setText("" + cachesize);
textview2.setText("" + datasize);
Class clazz = getClass().getClassLoader().loadClass("android.widget.AppSecurityPermissions");
Constructor constructor = clazz.getConstructor(new Class[] { Context.class, String.class });
Object object = constructor.newInstance(new Object[] { this, name });
Method method = clazz.getDeclaredMethod("getPermissionsView", new Class[] {});
View view = (View) method.invoke(object, new Object[] {});
relativelayout.addView(textview1, layoutParams1);
relativelayout.addView(textview2, layoutParams1);
relativelayout.addView(view);
setContentView(relativelayout);
} catch (Exception e) {
e.printStackTrace();
}
}
}
在这里基本告诫,最后上上效果图,写的有点啰嗦,是为了照顾一些跟我一样的菜鸟,希望大家不要喷,谢谢
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories