首页>>移动互联网>>Android 创建shortcut快捷方式
Android 创建shortcut快捷方式
来源: 时间: 2015-02-09 11:54:04

判断是否已经创建快捷方式:

public int checkShortCut(String appName) {

ContentResolver resolver = context.getContentResolver();

String author;

/*if (getSystemVersion() < 8) {

author = "com.android.launcher.settings";

} else {

author = "com.android.launcher2.settings";

}*/

author = this.getAuthorityFromPermission(context, "com.android.launcher.permission.READ_SETTINGS");

if(TextUtils.isEmpty(author)) {

author = this.getAuthorityFromPermission(context, "com.android.launcher.permission.WRITE_SETTINGS");

}

if(TextUtils.isEmpty(author)) {

return -1;

}

final Uri CONTENT_URI = Uri.parse("content://" + author

+ "/favorites?notify=true");

MLog.d("ShortCutUtils",CONTENT_URI.toString());

try {

Cursor c = resolver.query(CONTENT_URI, null, "title=?",

new String[] { appName }, null);

if (c == null || c.getCount() == 0) {

return 0;

} else {

c.close();

return 1;

}

} catch (Exception e) {

// 如果未发现就当是已经创建了

e.printStackTrace();

return 1;

}

}

创建快捷方式:

public void addShortCut(int type, boolean needRecord) {

Intent intent = new Intent(

"com.android.launcher.action.INSTALL_SHORTCUT");

intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);

intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, MobageResource

.getInstance().getString("mbga_content_description"));

// 是否可以有多个快捷方式的副本,参数如果是true就可以生成多个快捷方式,如果是false就不会重复添加

intent.putExtra("duplicate", false);

Intent intent2 = new Intent(Intent.ACTION_VIEW);

// 删除的应用程序的ComponentName,即应用程序包名+activity的名字

intent2.setComponent(new ComponentName(context.getPackageName(),

"com.mobage.android.MobageActivity"));

// Bundle bundle = new Bundle();

// bundle.putString(LaunchDashBoardItem.LAUNCH_URL,

// "http://mobage.cn/_game_top");

// bundle.putInt(LaunchDashBoardItem.OPEN_URL,

// LaunchDashBoardItem.SHOW_LAUNCH_URL);

// intent2.putExtra(LaunchDashBoardItem.EXTRA_KEY, bundle);

intent2.putExtra("ShortCut", true);

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent2);

intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,

Intent.ShortcutIconResource.fromContext(

context,

context.getResources().getIdentifier("mobage_icon",

"drawable", context.getPackageName())));

context.sendBroadcast(intent);

}

本页地址:http://www.easytd.com/yidonghulianwang/d_15020911266.html

上一篇:Android获取view在屏幕中的位置 下一篇:Android 无法连接SSL(https)进行网络通信