编程小部件添加到主屏幕在Android中 [英] programmatically add the widget to home screen in android

查看:142
本文介绍了编程小部件添加到主屏幕在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了Android小工具应用程序,其工作的罚款。
割我的客户问,当用户安装在我的这个程序,它会自动需要放在主屏幕顶部位置。如何做到这一点?请帮助我。

i have developed android widget app and its working fine. Mow my client ask, when the user installed my this app it automatically need to place on homescreen top position. how to do this? please help me.

推荐答案

请参照 http://viralpatel.net/blogs/android-install-uninstall-shortcut-example/

Android版为我们提供了一个意图类com.android.launcher.action.INSTALL_SHORTCUT可用于快捷方式添加到主屏幕。在以下code段,我们创建一个名为HelloWorldShortcut活动MainActivity的一条捷径。

Android provide us an intent class com.android.launcher.action.INSTALL_SHORTCUT which can be used to add shortcuts to home screen. In following code snippet we create a shortcut of activity MainActivity with the name HelloWorldShortcut.

首先,我们需要添加权限INSTALL_SHORTCUT到Android清单XML。

First we need to add permission INSTALL_SHORTCUT to android manifest xml.

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

该addShortcut()方法创建主页屏幕上的一个新的快捷方式。

The addShortcut() method create a new shortcut on Home screen.

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);
}

请注意我们如何创建一个保存我们的目标活动shortcutIntent对象。这个意图对象添加到另一个意图是EXTRA_SHORTCUT_INTENT。最后,我们播出新的意图。这增加提到与名称的快捷方式
EXTRA_SHORTCUT_NAME和EXTRA_SHORTCUT_ICON_RESOURCE定义图标。
注意:有一件事值得一提的是在这里,当你定义你的活动是从快捷调用,你必须定义的android:在标签出口=true属性。

Note how we create shortcutIntent object which holds our target activity. This intent object is added into another intent as EXTRA_SHORTCUT_INTENT. Finally we broadcast the new intent. This adds a shortcut with name mentioned as EXTRA_SHORTCUT_NAME and icon defined by EXTRA_SHORTCUT_ICON_RESOURCE. Note: One thing worth noting here is when you define your activity that is invoked from shortcut, you must define android:exported="true" attribute in tag.

这是卸载快捷方式从主屏幕上:

An To Uninstall Shortcut from Home screen:

安装,卸载或删除的Andr​​oid快捷键使用类似的Intent(UNINSTALL_SHORTCUT)来执行任务。在以下code我们删除主屏幕上添加快捷方式。

Similar to install, uninstalling or removing shortcut in Android uses an Intent (UNINSTALL_SHORTCUT) to perform the task. In following code we remove the shortcut added on home screen.

同样,我们需要执行卸载快捷方式的任务的权限。添加以下权限到Android清单XML。

Again we need a permission to perform uninstall shortcut task. Add following permission to Android manifest xml.

<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

该removeShortcut()方法不正是扭转addShortcut的()。大部分的code是除了removeShortcut呼吁UNINSTALL_SHORTCUT意图相似。

The removeShortcut() method does exactly reverse of addShortcut(). Most of the code is similar except removeShortcut calls UNINSTALL_SHORTCUT intent.

private void removeShortcut() {

    //Deleting 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
            .setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
    getApplicationContext().sendBroadcast(addIntent);
}

,你可以试试这个演示这里

这篇关于编程小部件添加到主屏幕在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆