【问题标题】:Android Shortcut is not working in VIVO 10Android 快捷方式在 VIVO 10 中不起作用
【发布时间】:2020-12-29 09:50:20
【问题描述】:

我在我的应用程序中实现了快捷方式,但这在 VIVO 10(Funtouch OS)中不起作用。请帮我。我已经使用了很多方法,但没有成功。

方法1:我正在使用此代码但无法正常工作

@SuppressLint("NewApi")
private fun shortcut(){
    val shortcutManager = getSystemService(ShortcutManager::class.java)
    val nhentaiIntent = Intent(this, MainActivity::class.java)
    nhentaiIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    nhentaiIntent.action = Intent.ACTION_VIEW
    if (shortcutManager!!.isRequestPinShortcutSupported) {
        val pinShortcutInfo = ShortcutInfo.Builder(this, "my-shortcut")
                .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
                 .setShortLabel("hello Shortcut")
                .setIntent(nhentaiIntent)
                .build()
        val pinnedShortcutCallbackIntent = shortcutManager.createShortcutResultIntent(pinShortcutInfo)
        val successCallback = PendingIntent.getBroadcast(this, /* request code */ 0, pinnedShortcutCallbackIntent, /* flags */ 0)

        shortcutManager.requestPinShortcut(pinShortcutInfo, successCallback.intentSender)
    }

}

方法2:我也用过这段代码,但是不行

Intent nhentaiIntent = new Intent(context, MainActivity.class);
    nhentaiIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    nhentaiIntent.setAction(Intent.ACTION_VIEW);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
        if (shortcutManager.isRequestPinShortcutSupported()) {
            ShortcutInfo info = new ShortcutInfo.Builder(context, "Shortcut")
                    .setIntent(nhentaiIntent)
                    .setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher))
                    .setShortLabel("Hello ")
                    .setLongLabel("Lable")
                    .build();
            Intent addIntent = shortcutManager.createShortcutResultIntent(info);
            shortcutManager.requestPinShortcut(info, PendingIntent.getBroadcast(context, 0, addIntent, 0).getIntentSender());
            Toast.makeText(context, "supported_launcher", Toast.LENGTH_LONG).show();
        } else {
            // TODO: Maybe implement this for launchers without pin shortcut support?
            // TODO: Should be the same with the fallback for Android < O
            // for now just show unsupported
            Toast.makeText(context, "unsupported_launcher", Toast.LENGTH_LONG).show();
        }
    }

方法3:我也用过这段代码,但不起作用

private fun addShourcut() {
    if (ShortcutManagerCompat.isRequestPinShortcutSupported(applicationContext)) {
        val shortcutInfo =
            ShortcutInfoCompat.Builder(applicationContext, "#1")
                .setIntent(
                    Intent(applicationContext, MainActivity::class.java).setAction(
                        Intent.ACTION_MAIN
                    )
                ) // !!! intent's action must be set on oreo
                .setShortLabel("Test")
                .setIcon(
                    IconCompat.createWithResource(
                        applicationContext,
                        R.mipmap.ic_launcher
                    )
                )
                .build()
        ShortcutManagerCompat.requestPinShortcut(applicationContext, shortcutInfo, null)
    } else {
        Toast.makeText(
            this@MainActivity, "launcher does not support short cut icon",Toast.LENGTH_LONG).show()
    }
}

请任何我在此问题上花费更多时间的人帮助我。

【问题讨论】:

  • 你找到解决办法了吗?

标签: android keyboard-shortcuts shortcut android-shortcut android-shortcutmanager


【解决方案1】:

我发现vivo设备的默认启动器中没有创建桌面快捷方式。当我将默认启动器更改为其他启动器时,它可以让我创建桌面快捷方式。到目前为止,我进行了研发,发现下面的解决方案不是完整的解决方案,但您可以使用以下代码重定向以允许桌面快捷方式屏幕。

val sintent = Intent()
sintent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
sintent .component = ComponentName("com.bbk.launcher2", "com.bbk.launcher2.installshortcut.PurviewActivity")
activity.startActivity(sintent )

如果用户允许或不允许,我尝试获取结果,但它不适用于所有 VIVO 设备。

var uri = Uri.parse("content://com.bbk.launcher2.settings/favorites")
            var query = activity.contentResolver.query(uri, null, " itemType = ?", arrayOf("30"), null);
            if (query != null && query.count > 0) {
                var idIndex = query.getColumnIndexOrThrow("_id");
                var intentIndex = query.getColumnIndexOrThrow("intent");
                var shortcutPermissionIndex = query.getColumnIndexOrThrow("shortcutPermission");
                while (query.moveToNext()) {
                    val long = query.getLong(idIndex)
                    val intent = query.getString(intentIndex)
                    val shortcutPermission = query.getInt(shortcutPermissionIndex)
                    var unflattenFromString = ComponentName.unflattenFromString(intent)
                    Log.e("bhavin->", "initView: getShortcutPerBtn id=$long packageName= ${unflattenFromString!!.packageName} shortcutPermission= $shortcutPermission")
                    if (unflattenFromString!!.packageName == activity.packageName && shortcutPermission != 0 && shortcutPermission != 16){
                        val intent = Intent()
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        intent.component = ComponentName("com.bbk.launcher2", "com.bbk.launcher2.installshortcut.PurviewActivity")
                        activity.startActivity(intent)
                        query.close()
                        return@ensureBackgroundThread
                    }
                    else{
                        Log.e("bhavin->","abcd")
                    }
                }
                query.close()
            }

有关更多详细信息,您可以查看我找到了这个git

注意:我发现一些设备 vivo 1902 和 Z3 存在异常

 java.lang.SecurityException: Permission Denial: opening provider
 com.bbk.launcher2.data.LauncherProvider from ProcessRecord{f8f30af  
 16137:com.qiutinghe.change/u0a172} (pid=16137, uid=10172) requires
 com.bbk.launcher2.permission.READ_SETTINGS or
 com.bbk.launcher2.permission.WRITE_SETTINGS

如果有人发现如何在 Vivo 设备中检查桌面权限,请添加到此答案中。

【讨论】:

    猜你喜欢
    • 2015-05-07
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多