SJonesGSO
SJonesGSO

Reputation: 93

Making a shortcut to a class (not the MainActivity) on the Android Homescreen

I've been looking ALL DAY to no avail..I know there are several questions on this but I haven't been able to find one that works properly for me.

Basically, I have an activity (MainActivity), with a function to add a shortcut to the home screen. When I click the shortcut, I want it to open a different activity (NewActivity). The hold up here seems to be that it doesn't work right when I try to launch a different activity than the one from which the shortcut was made.

The shortcut is successfully made, but when I click it I get an "App isn't installed" toast message from the Android system.

Here's the code for adding the shortcut:

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

EDIT: Here's my manifest:

<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 functions perfectly (I know because I've debugged it a lot), so that's not the problem.

Upvotes: 2

Views: 1653

Answers (2)

Anand Sainath
Anand Sainath

Reputation: 1807

The other way that you can do this is by setting the android:exported flag to true.

Upvotes: 3

FoamyGuy
FoamyGuy

Reputation: 46846

Try making your manifest look like this for the NewActivity entry:

    <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>

also maybe try making the shortcut intent like this:

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

Upvotes: 6

Related Questions