Reputation: 3417
I'm trying to develop a custom launcher that will contain the shortcut of my installed application (or even apk files). I can create a shortcut BUT outside the application and what I need to do is to create it inside my launcher activity. How can I do that?
AdminActivity.java (This activity contains a method that has the ability to create shortcut of the installed app from the device)
private boolean installUninstall_ShortCut(Context context, String appName, boolean isTrue) {
boolean flag =false ;
int app_id=-1;
PackageManager pm = context.getPackageManager();
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> res = pm.queryIntentActivities( i,0);
for(int k=0; k<res.size(); k++) {
if(res.get(k).activityInfo.loadLabel(pm).toString().equals(appName)){
flag = true;
app_id = k;
break;
}
}
if(flag) {
ActivityInfo ai = res.get(app_id).activityInfo;
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
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, appName);
intent.putExtra("duplicate", false);
if(isTrue) {
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher));
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
} else {
intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
}
context.sendBroadcast(intent);
} else
System.out.println("applicaton not found");
return true;
}
activity_launcher.xml (This is the Layout of Launcher Activity where the shortcut will go. I really don't know how to bring the created shortcut in this activity)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/plain_without_logo"
tools:context=".S_2nd_MainActivity" >
<GridView
android:id="@+id/grid_app"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="80dp"
android:gravity="center"
android:horizontalSpacing="15dp"
android:numColumns="auto_fit"
android:padding="10dp"
android:stretchMode="columnWidth"
android:verticalSpacing="15dp" />
Upvotes: 1
Views: 1947
Reputation: 21733
What you are creating is not a launcher like the home screen, its just an application. The launcher category refers to the activity being listed in the all apps drawer. I don't think applications can add their own shortcuts otherwise spam ware could just keep adding its icon to your home screen like this and annoy every user.
Also installUninstall_ShortCut is not how we name things in Java! Actually it doesn't match any naming convention I've ever come across
If you want to mimic adding-shortcut-like functionality then you can get the grid view and call addView on it
Upvotes: 1