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

标签 android eclipse android-intent android-emulator

我在主屏幕上创建了应用程序快捷方式。但它不工作。它总是显示 toast app isn't installed。我尝试了 2 种不同的代码,但结果始终相同。

这是第一个代码

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

第二个代码

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

记录猫和 list :

    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>

我已经在带有 api 18 的模拟器和带有 android 4.2 的平板电脑上尝试了我的应用

任何帮助将不胜感激。 谢谢

最佳答案

在我的例子中添加

android:exported="true"

从启动器调用的 MainActivity 有效。 如果有帮助。

关于android - 主屏幕应用程序快捷方式不适用于 android。应用程序未安装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21542409/

相关文章:

android - iphone 开发人员的 android 开发指南?

java - 查看 Drag Helper 助手

eclipse - 如何从 .zip 安装 Eclipse 插件

c - 为什么在嵌套的 'for' 循环中使用相同的变量作为索引变量不会引发警告?

android - 以相同的 Intent 重新启动 Activity 时如何避免重复处理

android - 从 Google Play 但不是从 Android Studio 运行时出现 NullPointerException

javascript - 播放来自 YouTube 的视频时出现黑屏 : Phonegap Android

java - 必应翻译 API 无法正常工作

Android 搜索小部件 - onQueryTextSubmit 和将 Intent 发送到 SearchableActivity 之间的区别?

android - 在 Google map 中标记 Recyclerview 数据