Android : Remove all shortcuts associated with the app from code - android

I want to remove all the shortcuts on home screen of my app on a single event like button click.
Is there any way to do this ? or at least get the display-names of all the shortcuts placed on home screen from my app ?
( I cant have it in any shared-prefs / db while creation of shortcuts because user can even do a 'clear-data' )

Try this:
appShortcutIntent = new Intent();
appShortcutIntent.setClassName("com.pkg.pkgname", "MainActivity");
appShortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
appIcon = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
Intent intentDeleteShortcut = new Intent();
intentDeleteShortcut.putExtra("android.intent.extra.shortcut.INTENT", appShortcutIntent);
intentDeleteShortcut.putExtra("android.intent.extra.shortcut.NAME", getResources().getString(R.string.app_name));
intentDeleteShortcut.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", appIcon);
intentDeleteShortcut.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
sendBroadcast(intentDeleteShortcut);
And add this permission in manifest file:
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
Hope this helps.
(P.S: This will require the device to be rooted as far as I know.)

Related

How to make shortcut on android launcher for hybrid app?

I want to make it easy to add website shortcut to home screen by pressing a button. So What I am Thinking is a button at the bottom of my hybrid app that says "Add to home screen ? yes or no" and when "yes" pressed, it adds the shortcut to the home screen without closing the application. what code should I add To do that?
here is the code that creates a web browser shortcut:
Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Goto http://www.example.com/link");
Parcelable icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.shortcut_icon);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new
Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com/link")));
sendBroadcast(shortcutIntent);
here is the permission you need to add:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

Add HomeScreen WebApps from another application? [duplicate]

Do you know is there a programmatic way to create a web shortcut on the phone user's home screen?
What I want to do is:
When the phone user clicks a button in our Android application, the application will then place a website shortcut onto the phone user's home screen.
First you'll need to add a permission to your manifest.xml
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT">
</uses-permission>
You'll need to build an intent to view the webpage. Something like...
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://www.blablaba.com"));
You can test this by creating a little test app and doing startActivity(i); This should open the browser. Once you verified that the above intent is correct you should move on to the next step.
Now you'll need to actually install the shortcut.
Intent installer = new Intent();
installer.putExtra("android.intent.extra.shortcut.INTENT", i);
installer.putExtra("android.intent.extra.shortcut.NAME", "THE NAME OF SHORTCUT TO BE SHOWN");
installer.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", I THINK this is a bitmap); //can also be ignored too
installer.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(installer)
;
It's also possible some home screens don't accept this, but most do. So enjoy.
EDIT:
Icon can be set to the shortcut using:
installer.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", Intent.ShortcutIconResource.fromContext(mContext, R.drawable.icon));
As an addition to the correct answer by #Mike-dg and #Gagan, instead of using
putExtra("android.intent.extra.shortcut.ICON_RESOURCE", Intent.ShortcutIconResource.fromContext(mContext, R.drawable.icon))
which requires a ShortcutIconResource, you can use
putExtra("android.intent.extra.shortcut.ICON", Bitmap))
which lets you use any bitmap as the icon. This makes it easy to use a the favicon of the shortcut's website as the icon.
You can't place UI elements on the phone's home screen through an .apk. For the web shortcut - you can create a widget that opens the browser (with the specified URL) upon click.

How can I add my application's shortcut to the homescreen upon app installation? [duplicate]

This question already has answers here:
How can I place app icon on launcher home screen?
(6 answers)
Closed 5 years ago.
I want to create my app's shortcut/launcher icon on the homescreen as soon as I install my app (even before I start it). Is that possible? How might I do that?
Since ICS, you can do like this:
public void createShortCut(){
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutintent.putExtra("duplicate", false);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), EnterActivity.class));
sendBroadcast(shortcutintent);
}
Please also refer to the source code of launcher at: this link
Edit : If somebody would miss reading comment so adding following line.
This requires "com.android.launcher.permission.INSTALL_SHORTCUT" permission
You forgot to add permissions:
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission
android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
Great tutorial here:
http://androidforbegineers.blogspot.in/2014/01/android-home-screen-shortcut.html#!
NOTE: This answer is now wrong. See, for instance, Robin's answer for a way to do this.
As far as I know, an app cannot force itself onto the home screen. It gets added to the app list that the launcher app maintains, but the home screen is generally under user control. Giving apps the ability to clutter up the home screen would be an open invitation for abuse.
If you install your application, neither your application, nor any services or other processes are active! Android want's the user to make the first step in "opening" the application. You are not able to install a shortcut directly! Thats the answer. Notice: On most devices the launcher will create a shortcut for your application itself.
Can you install shortcuts in runtime after the user has somehow started your app at least once? : yes (see Robins answer).
Is there a workaround?
Maybe, but no good one.
Update, Another hint: If you have already an application on the device of the user. (For example if your second app that gets installed is the key for "going pro" or something), then you actually can start the second application from the first one, without the user has started the second app ever (and add a shortcut).
Create function to call intent of shortcut:
private void criarAtalho() {
Intent shortcutIntent = new Intent(getApplicationContext(), SplashScreen.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "nomeDaApp");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.mipmap.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false); //may it's already there so don't duplicate
getApplicationContext().sendBroadcast(addIntent);
}
write on OnCreat the call of function:
if(!getSharedPreferences("APP_PREFERENCE", Activity.MODE_PRIVATE).getBoolean("IS_ICON_CREATED", false)){
criarAtalho();
getSharedPreferences("APP_PREFERENCE", Activity.MODE_PRIVATE).edit().putBoolean("IS_ICON_CREATED", true).commit();
}
set the permissions on manifest:
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
First, add permission for adding shortcut to manifest:
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Then, call the function below.
public void createShortcut(){
Intent intentShortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
intentShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
Parcelable appicon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher);
intentShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, appicon);
intentShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), MainActivity.class));
intentShortcut.putExtra("duplicate", false);
sendBroadcast(intentShortcut);
}

Creating a unique shortcut when installing an app behaves differently on different Android version

I use the following codes to create a shortcut when installing an app:
in AndroidManifest.xml:
<!-- for creating a shortcut in the home screen -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
in onCreate() of the main activity:
// an Intent to create a shortCut
Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//repeat to create is forbidden
shortcutIntent.putExtra("duplicate", false);
//set the name of shortCut
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, this.getString(R.string.app_name));
//set icon
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
//set the application to lunch when you click the icon
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT
, new Intent(getApplicationContext() , MainActivity.class));
//sendBroadcast,done
sendBroadcast(shortcutIntent);
These codes work fine in Android 4.0.4, which creates an shortcut at the first time and send a toast saying the shortcut already exists after the 1st time installation. But in Android 4.2.2, I can create many duplicated shortcuts by clicking the back key and enter the app again.
Is there any way to work on both version of Android ?
Thanks in advance :)
shortcutIntent.putExtra("duplicate", false) is not working in 4.2 version. i think you have to use some other ways to create unique shortcut.
If failour then just uninstall the shortcut and install again.something like :
addIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
and in manifest use both UNINSTALL_SHORTCUT and INSTALL_SHORTCUT permissions.
And this rough idea is nicely working on every versions.

How to add bookmarks(URL) on the home screen for android

I have tried adding a bookmark on the home screen manually. Need some help on doing it programatically. Thanks.
This works for me -
Add to permission to manifest
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Use the following code:
public void createGoogleSearchShortcut(Context context) {
String urlStr = String.format(context.getString(R.string.homescreen_shortcut_search_url), context.getString(R.string.app_id));
Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlStr));
// shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// Sets the custom shortcut's title
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.search));
// Set the custom shortcut icon
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_action_search));
intent.putExtra("duplicate", false);
// add the shortcut
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
context.sendBroadcast(intent);
}
Please note: I strongly suggest asking the user before adding the bookmark. Adding bookmarks to his home screen without his permission is "not polite"...
I'm not sure what you mean by bookmark. Maybe this tutorial could help?
http://www.tutorialforandroid.com/2009/04/open-urlwebsite-from-android.html
This is an application that starts up the browser pointing to a web page. Is this what you had in mind?
Emmanuel
Check out the (poorly documented) action INSTALL_SHORTCUT. You'll need the respective permission, as explained in that link

Categories

Resources