Reputation: 11768
I want the user to be able to see my application in the menu only until he configurates it after that i want to remove the shortcut also i would like to put a shortcut on the users desktop.I am pretty new to java
It's there a way I can acheive this ?
Upvotes: 0
Views: 120
Reputation: 46856
I don't know of a way that you could have it appear in the app drawer but remove it later.
To create a shortcut on the home screen you can use the following method.
PLEASE NOTE - I believe The intent that is being broadcast here, and the permission that you must request in your manifest are not a part of the public APIs. This method will not work on every home screen implementation. This method will potentially stop working at any time (and may already not work on newer versions of android, I only tested on Nexus S, but it does seem to work on that device).
You have been warned
public boolean setShortCut(Context context, String appName)
{
System.out.println("in the shortcutapp on create method ");
boolean flag =false ;
int app_id=-1;
PackageManager p = context.getPackageManager();
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> res =p.queryIntentActivities( i,0);
System.out.println("the res size is: "+res.size());
for(int k=0;k<res.size();k++)
{
System.out.println("the application name is: "+res.get(k).activityInfo.loadLabel(p));
if(res.get(k).activityInfo.loadLabel(p).toString().equals(appName)){
flag = true;
app_id = k;
break;
}
}
if(flag){
ActivityInfo ai = res.get(app_id).activityInfo;
Intent shortcutIntent = new Intent();
shortcutIntent.setClassName(ai.packageName, ai.name);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "SIZETESTDYNAMIC");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));
//intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
context.sendBroadcast(intent);
System.out.println("in the shortcutapp on create method completed");
}
else
System.out.println("appllicaton not found");
return true;
}
and you'll have to add this permission to your manifest:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
Upvotes: 1