launcher相关小功能

最近在做launcher的开发,收集到很多细节小功能,分享下:

1、获取本机所有已安装的应用

public static List<ResolveInfo> mApps;

<pre name="code" class="java">	/**
	 * 通过PackageManager的api查询已经安装的apk
	 */
	private void loadApps() {
		Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
		mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

		mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
	}

2、如何访问应用

 
上面已经获取到所有应用,显示到列表后监测点击再加上简单两行代码,便可以轻松访问
<pre name="code" class="java"><span style="white-space:pre">	</span>/**
	 * 我们根据position即可知道被点击的项目是哪一项了。现在我们根据被点击的项目,取出对应的应用程序数据(主要是其中的主activity),
	 * 然后启动activity
	 */
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		ResolveInfo info = mApps.get(position);

		// 该应用的包名
		String pkg = info.activityInfo.packageName;
		// 应用的主activity类
		String cls = info.activityInfo.name;
		ComponentName componet = new ComponentName(pkg, cls);
		Log.i("package", pkg);
		Log.i("package", cls);
		Intent i = new Intent();
		i.setComponent(componet);
		startActivity(i);
	}

3、如何利用pm命令执行操作
 
<pre name="code" class="java"><span style="white-space:pre">	</span>/**
	 * 基于pm命令的卸载app的方式
	 */
	private void pmcHideTheApp(String str) {
		// adb push core code
		String command = "pm uninstall " + str;
		Process process = null;
		DataOutputStream os = null;
		try {
			process = Runtime.getRuntime().exec("su");// 设备必须有root权限,才能执行命令,这里是获取root权限
			os = new DataOutputStream(process.getOutputStream());
			os.writeBytes(command + "\n");
			os.writeBytes("exit\n");
			os.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值