您的位置:首页 > 移动开发 > Android开发

Android:创建快捷方式

2012-03-19 20:00 176 查看
Java代码

/**
* 创建快捷方式到Shortcut列表<br>
* 关联程序的<intent-filter>中添加:<actionandroid:name="android.intent.action.CREATE_SHORTCUT"/>
*/
private void addShortcutToOptions() {
Intent shortcut = new Intent(Intent.ACTION_CREATE_SHORTCUT);
// 不允许重建
shortcut.putExtra("duplicate", false);
// 设置名字
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
this.getString(R.string.app_name));
// 设置图标
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(this,
R.drawable.ic_launcher));
// 设置意图和快捷方式关联的程序
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
new Intent(this, this.getClass()));
// 发送消息队列
setResult(RESULT_OK, shortcut);
}

/**
* 添加快捷方式到桌面 要点:
* 1.给Intent指定action="com.android.launcher.INSTALL_SHORTCUT"
* 2.给定义为Intent.EXTRA_SHORTCUT_INENT的Intent设置与安装时一致的action(必须要有)
* 3.添加权限:com.android.launcher.permission.INSTALL_SHORTCUT
*/
private void addShortcutToDesktop() {
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
// 不允许重建
shortcut.putExtra("duplicate", false);
// 设置名字
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,this.getString(R.string.app_name));
// 设置图标
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(this,
R.drawable.ic_launcher));
// 设置意图和快捷方式关联程序
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,new Intent(this, this.getClass()).setAction(Intent.ACTION_MAIN));
// 发送消息
sendBroadcast(shortcut);
}

/**
* 添加权限:<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
*
*/
private boolean hasInstallShortcut() {
boolean hasInstall = false;
final String AUTHORITY = "com.android.launcher.settings";
Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/favorites?notify=true");
Cursor cursor = this.getContentResolver().query(CONTENT_URI,
new String[] { "title", "iconResource" }, "title=?",
new String[] { this.getString(R.string.app_name) }, null);
if (cursor != null && cursor.getCount() > 0) {
hasInstall = true;
}
return hasInstall;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android string action null java