android 创建快捷桌面

    private AlertDialog mAlertDialog;
    /**
     * 创建快捷icon对话框
     */
    private void createShortcutDialog() {
        AlertDialog.Builder dialog = new AlertDialog.Builder(FingerPaint.this);
        dialog.setTitle(getString(R.string.shortcut_dialog_title));
        dialog.setMessage(getString(R.string.shortcut_dialog_message));
        dialog.setIcon(R.drawable.dialog_logo);
        dialog.setCancelable(false);
        dialog.setPositiveButton(getString(R.string.shortcut_dialog_ok), new DialogInterface.OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
                createShortCut(R.drawable.ic_launcher,R.string.app_name,".MainActivity");//MainActivity is your launcher activity
                
                finish();
            }
        });
        dialog.setNegativeButton(getString(R.string.shortcut_dialog_cancel), new DialogInterface.OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
                
                finish();
            }
        });
        mAlertDialog = dialog.create();
        mAlertDialog.show();
    }
    
    private static final String ACTION_INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
    static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";


    /**
     * 是否可以有多个快捷方式的副本
     */
    private void createShortCut(final int iconId, final int stringId, final String activityName) {
        if (activityName == null || TextUtils.isEmpty(activityName))
            return;
        // 创建快捷方式的Intent
        Intent shortcutintent = new Intent(ACTION_INSTALL_SHORTCUT);
        // 不允许重复创建
        shortcutintent.putExtra(EXTRA_SHORTCUT_DUPLICATE, false);
        // 需要现实的名称
        shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(stringId));
        // 快捷图片
        Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), iconId);
        shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
        // 点击快捷图片,运行的程序主入口
        Intent intent2 = new Intent();
        intent2.setAction("android.intent.action.VIEW");
        intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent2.setAction(Intent.ACTION_MAIN);
        // 包名
        String packageName = this.getPackageName();
        intent2.setComponent(new ComponentName(packageName, packageName + activityName));
        shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent2);
        // 发送广播。OK
        sendBroadcast(shortcutintent);

    }


当然权限少不了

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

你可能感兴趣的:(android,String,null,dialog,action)