0

I think that title said everything. I want only create shortcut on home screen beacause, I see more and more apps placing their shortcuts on the home screen after they got installed. I tried some codes but dont work. There are my activity and manifest file :

My Activity :

import com.files.getquote.R;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;


public class GetQuoteActivity extends Activity {

    WebView myBrowser;
;
    /** Called when the activity is first created. */
    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myBrowser = (WebView)findViewById(R.id.mybrowser);

        myBrowser.loadUrl("file:///android_asset/index.html"); 

        myBrowser.getSettings().setDatabasePath("/data/data/" + myBrowser.getContext().getPackageName() + "/databases/");

        myBrowser.getSettings().setDomStorageEnabled(true);

        myBrowser.getSettings().setJavaScriptEnabled(true);

        myBrowser.getSettings().setDatabaseEnabled(true);


    }

}

AndroidManifest.xml :

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.files.getquote"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />
<uses-permission
        android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    <application android:icon="@drawable/ic_launcher"  android:label="@string/app_name">
        <activity android:name=".GetQuoteActivity" android:theme="@android:style/Theme.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>
George Simon
  • 75
  • 1
  • 2
  • 8

2 Answers2

4

In order to add a shortcut to the home screen in Android, you need to do something like the following:

Manifest

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

Code

Somewhere in your app (probably best to place in a method that you can call, when the app starts?)

Intent shortcut = new Intent(getApplicationContext(), MainActivity.class);
shortcut.setAction(Intent.ACTION_MAIN);

Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcut);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "YOUR APP SHORTCUT TEXT");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),                                         
    R.drawable.ic_launcher));
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(intent);

You will need a receiver to accept the broadcast. But this should point you in the right direction to start with. You also need to specify android:exported="true" - when wanting to start an app from a shortcut, in your manifest. Kept me guessing for hours that one! :)

And for extra clarification on the topic: Add Shortcut for android application To home screen On button click

Community
  • 1
  • 1
laminatefish
  • 5,197
  • 5
  • 38
  • 70
  • Thanks but can you explain it or give example vhere should i put the code intent shortcut? – George Simon Feb 20 '14 at 16:09
  • What would you like explained about it? I thought I was quite clear. Basically. Put that in a method. Called something like : createShortcut(). Then (to test) in you main activity. Stick a call to createShortcut() and check that you can create one in the first place. Then you'll have to decide where the best place to put this is. As I've said you will need a receiver to catch it. If you need help with receivers: http://developer.android.com/reference/android/content/BroadcastReceiver.html - has all the info you could want :) – laminatefish Feb 20 '14 at 16:16
  • @LokiSinclair .. Thank you for your solution. It works fine for me. But I have another issue. My app supports multiple language. So, when I change the phone language in settings, my app name gets updated fine in launcher. But the app name does not update to new language in home screen. Do I need to add somethings more for that? Thanks for reading.. – Sushil Mar 20 '14 at 00:51
  • In that instance I'd remove the shortcut you created and create another after the fact. I don't know of any way to update it once created. Someone else may well know however, cheers! – laminatefish Mar 20 '14 at 09:12
0

You can use this to create a shortcut:

Intent shortcutIntent = new Intent (this, YourActivity.class);      
Intent addIntent = new Intent(); 
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Title"); 
addIntent.putExtra("duplicate", false); 
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon)); 
addIntent.   
sendBroadcast(addIntent);
FD_
  • 12,947
  • 4
  • 35
  • 62
  • Thank you for your solution. It works fine for me. But I have another issue. My app supports multiple language. So, when I change the phone language in settings, my app name gets updated fine in launcher. But the app name does not update to new language in home screen. Do I need to add somethings more for that? Thanks for reading.. – Sushil Mar 20 '14 at 00:52