【发布时间】:2014-12-03 08:49:52
【问题描述】:
我创建了一个小部件应用程序。现在我希望它直接显示在主屏幕上,在应用程序部署后立即显示,就像快捷方式一样,单击快捷方式小部件将转到应用程序。
有人可以推荐我吗?谢谢。
【问题讨论】:
我创建了一个小部件应用程序。现在我希望它直接显示在主屏幕上,在应用程序部署后立即显示,就像快捷方式一样,单击快捷方式小部件将转到应用程序。
有人可以推荐我吗?谢谢。
【问题讨论】:
首先我们需要将权限 INSTALL_SHORTCUT 添加到 android manifest xml。
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
private void addShortcut() {
//Adding shortcut for MainActivity
//on Home screen
Intent shortcutIntent = new Intent(getApplicationContext(),
MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.ic_launcher));
addIntent
.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
// 检查应用是否首次安装。
SharedPreferences prefs = getSharedPreferences("ShortCutPrefs", MODE_PRIVATE);
if(!prefs.getBoolean("isFirstTime", false)){
addShortcut();
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("isFirstTime", true);
editor.commit();
}
【讨论】: