简体   繁体   English

主屏幕应用程序快捷方式不适用于android。 应用未安装

[英]home screen app shortcut is not working android. app isn't installed

I have created app shortcut on home screen. 我已经在主屏幕上创建了应用程序快捷方式。 but Its not working. 但它不起作用。 it always shows the toast app isn't installed . 它始终表明app isn't installed Toast app isn't installed I have tried 2 different codes but the result is always same. 我尝试了2种不同的代码,但结果始终相同。

Here is the first code 这是第一个代码

private void addShortcut() {
        // Adding shortcut 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, "AppName");
        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);
    }

Second code 第二码

public static void addShortcut(Context context)
    {
        Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

        ApplicationInfo appInfo = context.getApplicationInfo();

        // Shortcut name
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "appInfo.name");
        shortcut.putExtra("duplicate", false); // Just create once

        // Setup activity shoud be shortcut object 
        ComponentName component = new ComponentName(appInfo.packageName, "MainActivity.class");
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(component));

        // Set shortcut icon
        ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(context, appInfo.icon);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

        context.sendBroadcast(shortcut);
    }

Log cat and manifest: 记录猫和清单:

    02-03 22:36:58.625: E/memtrack(1697): Couldn't load memtrack module (No such file or directory)
    02-03 22:36:58.625: E/android.os.Debug(1697): failed to load memtrack module: -2
    02-03 22:37:02.515: E/memtrack(1708): Couldn't load memtrack module (No such file or directory)
    02-03 22:37:02.515: E/android.os.Debug(1708): failed to load memtrack module: -2
    02-03 22:37:03.255: E/InputDispatcher(388): channel 'b500ff48 com.example.test_shortcut/com.example.test_shortcut.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
    02-03 22:37:13.805: E/WindowManager(388): Starting window AppWindowToken{b5537500 token=Token{b50334a8 ActivityRecord{b502b0e0 u0 com.example.test_shortcut/.MainActivity t7}}} timed out



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test_shortcut"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.test_shortcut.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name="com.example.test_shortcut.PackageReplacedReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_REPLACED" />

                <data
                    android:path="com.example.test_shortcut"
                    android:scheme="com.example" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

I have tried my app at emulator with api 18 and at tablet with android 4.2 我已经在带有api 18的模拟器和带有android 4.2的平板电脑上尝试了我的应用

Any help will be appreciated. 任何帮助将不胜感激。 Thanks 谢谢

In my case adding 就我而言

android:exported="true"

to MainActivity to be called from launcher worked. 从启动器调用MainActivity的工作。 If it helps. 如果有帮助。

You may need: 你可能需要:

<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REPLACED" />

Also replace: 同时替换:

 <intent-filter>
       <action android:name="android.intent.action.PACKAGE_REPLACED" />
       <data
                android:path="com.example.test_shortcut"
                android:scheme="com.example" />
 </intent-filter>

With: 带有:

 <intent-filter android:priority="999">
       <action android:name="android.intent.action.PACKAGE_REPLACED" />
       <data android:scheme="com.example.test_shortcut" />
 </intent-filter> 

I haven't actually tested this, let me know if it works now. 我还没有实际测试过,请告诉我现在是否有效。 It also wouldn't hurt to use your own custom package name to avoid conflicts with existing apps, instead of using the default package name, that starts with com.example , that's used for most examples, demos, and tutorials on Android. 使用您自己的自定义程序包名称来避免与现有应用程序冲突也不会受到伤害,而不是使用以com.example开头的默认程序包名称,该默认程序包名称用于Android上的大多数示例,演示和教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM