如何将我的应用程序快捷方式添加到主屏幕,如360安全

时间:2016-10-08 02:02:31

标签: java android android-studio

我想在主屏幕上创建我的应用程序的快捷方式/启动器图标,就像360安全性一样,当用户点击图标而不进入应用程序时发布活动

1 个答案:

答案 0 :(得分:1)

private void addShortcut() {

    //Adding shortcut for MainActivity
    //on Home screen
    Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
    shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    shortcutIntent.putExtra(Constants.DEVICE_NO, mDevice.getFDeviceNo());
    shortcutIntent.setAction(Intent.ACTION_MAIN);
    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, mDevice.getFName());
    int defaultImageRes = Utils.getDeviceImage(mDevice.getFViewType());
    int color = Color.RED;
    Bitmap defaultBitmap = overlay(BitmapFactory.decodeResource(getResources(), defaultImageRes), color);
    if (mDevice.isCustomImage() && !mDevice.getFImage().isEmpty()) {
        File file = MyApplication.getImageLoader(this).getDiskCache().get(ImageUtils.getImageUrl(mDevice.getFImageName()));
        }
        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
        if (bitmap != null) {
            Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, 128, 128, true);
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, scaledBitmap);
        } else {
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, defaultBitmap);
        }
    } else {
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, defaultBitmap);
    }

    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    getApplicationContext().sendBroadcast(addIntent);
    Toast.makeText(this, R.string.added_to_home_screen, Toast.LENGTH_SHORT).show();
}