Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create app shortcut inside a Custom launcher in Android?

Tags:

android

I'm currently developing an app that can create a shortcut of all installed application of the device. In order to create a shortcut, the user needs to log in as administrator and from that activity, there's a ListView that contains all the installed apps. The user needs to select app/s so that it will create shortcut.

private boolean Generate_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) {    
        // CREATE SHORTCUT  
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher));
        intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");       
    } else {
        // REMOVE SHORTCUT
        intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
    }

    context.sendBroadcast(intent);

} else
    System.out.println("applicaton not found");
return true;
}

After selecting an app, it will create a shortcut in Main Activity that has a GridView. The problem here is that the created shortcut is created in the Home Screen. Now my question is, how can I create a shortcut inside my application (MainActivity.java) instead in Home Screen?

like image 799
androidBoomer Avatar asked Jan 24 '14 03:01

androidBoomer


2 Answers

Did you try?

<activity android:name=".YourActivity" android:label="@string/app_name">
  <intent-filter>
    <action android:name="android.intent.action.CREATE_SHORTCUT" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>

Then in the activity that receives the intent, you create an intent for your shortcut and return it as the activity result.

// create shortcut if requested
ShortcutIconResource icon =
    Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);

Intent intent = new Intent();

Intent launchIntent = new Intent(this,ActivityToLaunch.class);

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, someNickname());
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

setResult(RESULT_OK, intent);
like image 126
Luc Avatar answered Oct 22 '22 00:10

Luc


It's possible to display an icon in your application that will launch another application. You could use the PackageManager to discover installed applications and obtain the launcher icon you should display.

  1. Get the PackageManager from your application's context.

    PackageManager pm = context.getPackageManager();
    
  2. Create an intent that will be used to query for the installed applications that exist and would be displayed in the launcher.

    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    
  3. Query the for any applications whose IntentFilter matches our mainIntent.

    List<ResolveInfo> apps = pm.queryIntentActivities(mainIntent, 0);
    
  4. Iterate the List of ResolveInfo that is returned to obtain the details necessary to create an Intent for launching the application and the resource to display as the launcher icon.

    for (ResolveInfo app : apps) {
        // Create an Intent that can be use to launch the application
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setComponent(new ComponentName(app.activityInfo.applicationInfo.packageName, app.activityInfo.name));
    
        // Get the icon to display in your UI
        Drawable icon = app.activityInfo.loadIcon(pm);
    }
    

UPDATE: You can find a walkthru of creating a custom launcher here: http://arnab.ch/blog/2013/08/how-to-write-custom-launcher-app-in-android/

like image 21
csmurphy84 Avatar answered Oct 22 '22 02:10

csmurphy84