Android桌面快捷方式的设置(一)

MainActivity如下:

package cn.testshortcut;

import java.util.List;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
/**
 * Demo描述:
 * 桌面快捷方式的设置
 * 
 * 备注说明:
 * 该方式只适合Android原生系统和非原生系统
 * 
 * 权限设置:
 * <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
 * <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
 * <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
 */
public class MainActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		addShortcutToDesktop(MainActivity.this);
	}
	
	public static void addShortcutToDesktop(Context context) {
		if (!hasShortcut(context)) {
			Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
			shortcutIntent.putExtra("duplicate", false);
			shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,context.getString(R.string.app_name));
			shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
			Intent.ShortcutIconResource.fromContext(context,R.drawable.ic_launcher));
			Intent intent = new Intent(context, MainActivity.class);
			intent.setAction("android.intent.action.MAIN");
			intent.addCategory("android.intent.category.LAUNCHER");
			shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
			context.sendBroadcast(shortcutIntent);
		}
	}

	private static boolean hasShortcut(Context context) {
		String AUTHORITY = getAuthorityFromPermission(context,"com.android.launcher.permission.READ_SETTINGS");
		System.out.println(" AUTHORITY ..." + AUTHORITY);
		if (AUTHORITY == null) {
			return false;
		}
		Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY+ "/favorites?notify=true");
		String title = "";
		final PackageManager packageManager = context.getPackageManager();
		try {
			title = packageManager.getApplicationLabel(
					packageManager.getApplicationInfo(context.getPackageName(),
					PackageManager.GET_META_DATA)).toString();
		} catch (NameNotFoundException e) {
			        e.printStackTrace();
		}
		Cursor c = context.getContentResolver().
				   query(CONTENT_URI,new String[] { "title" }, "title=?", new String[] { title },null);
		if (c != null && c.getCount() > 0) {
			return true;
		}
		return false;
	}

	private static String getAuthorityFromPermission(Context context,String permission) {
		if (TextUtils.isEmpty(permission)) {
			return null;
		}
		List<PackageInfo> packageInfoList = 
		context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);
		if (packageInfoList == null) {
			return null;
		}
		for (PackageInfo packageInfo : packageInfoList) {
			ProviderInfo[] providerInfos = packageInfo.providers;
			if (providerInfos != null) {
				for (ProviderInfo providerInfo : providerInfos) {
					if (permission.equals(providerInfo.readPermission)|| 
						permission.equals(providerInfo.writePermission)) {
						return providerInfo.authority;
					}
				}
			}
		}
		return null;
	}
}

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"
     >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="建立桌面快捷方式"
        android:layout_centerInParent="true" 
    />

</RelativeLayout>


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谷哥的小弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值