2

我一直在寻找一整天都无济于事..我知道对此有几个问题,但我无法找到一个适合我的问题。

基本上,我有一个活动(MainActivity),具有向主屏幕添加快捷方式的功能。当我单击快捷方式时,我希望它打开一个不同的活动(NewActivity)。这里的问题似乎是,当我尝试启动与创建快捷方式的活动不同的活动时,它无法正常工作。

快捷方式已成功创建,但当我单击它时,我会从 Android 系统收到一条“未安装应用程序”的 toast 消息。

这是添加快捷方式的代码:

                Intent shortcutIntent = new Intent();
                shortcutIntent.setClassName("com.enceladus.myApp", "com.enceladus.myApp.NewActivity");
                shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                Intent addIntent = new Intent();
                addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
                addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Calculator");
                addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(MainActivity.this, R.drawable.calc));
                addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
                MainActivity.this.sendBroadcast(addIntent);

编辑:这是我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.enceladus.myApp"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.enceladus.myApp.NewActivity"
        android:label=""
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Translucent.NoTitleBar"
        android:launchMode="singleInstance"
        />
</application>

NewActivity.class 功能完美(我知道是因为我已经调试了很多),所以这不是问题。

4

2 回答 2

6

尝试为 NewActivity 条目制作清单,如下所示:

    <activity android:name="com.enceladus.myApp.NewActivity"
    android:label=""
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Translucent.NoTitleBar"
    android:launchMode="singleInstance"
    > 
       <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

        </intent-filter>


    </activity>

也可以尝试制作这样的快捷方式意图:

Intent shortcutIntent = new Intent(MainActivity.this, NewActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN); //Not sure if you'll need this or not.
//remove the next line.
//shortcutIntent.setClassName("com.enceladus.myApp", "com.enceladus.myApp.NewActivity");
于 2012-11-25T01:39:24.293 回答
3

您可以执行此操作的另一种方法是将android:exported标志设置为 true。

于 2014-08-04T22:32:42.647 回答