how to create shortcut in android programatically

Common bugs/problems with the Android SDK the Emulator and the ADT-Plugin.

how to create shortcut in android programatically

Postby ranjit0046 » Mon Jan 04, 2010 2:07 pm

Hai friends ,
i want to create a "shortcut item " in my home screen . it is urgent for my project , please help me any body.



Ranjit.....................................
ranjit0046
Developer
Developer
 
Posts: 47
Joined: Mon Dec 14, 2009 12:46 pm

Top

short cut key

Postby dhaiwat » Tue Mar 09, 2010 8:37 am

hi

did you figure out that short cut key problem, if so than please share the step how get any key listener event when user press any where from.
dhaiwat
Developer
Developer
 
Posts: 28
Joined: Tue Feb 10, 2009 6:49 am

Re: short cut key

Postby ranjit0046 » Wed Mar 24, 2010 9:26 am

dhaiwat wrote:hi

did you figure out that short cut key problem, if so than please share the step how get any key listener event when user press any where from.



Hi Dhaiwat ,

I am sending a code snippet to you , once go through this and achieve your task .Just call the bellow function by passing parameters of which application you want to create shortcut and context object .



public static boolean setShortCut(String appName,Context context) throws UserException
{

try
{
//Log.i("shortcut method in androidhelper start","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++)
{
//Log.i("","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);
shortcutIntent.addCategory(Intent.ACTION_PICK_ACTIVITY);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// Sets the custom shortcut's title
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);

BitmapDrawable bd=(BitmapDrawable)(res.get(app_id).activityInfo.loadIcon(p).getCurrent());
Bitmap newbit;
newbit=bd.getBitmap();
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, newbit);

intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
context.sendBroadcast(intent);

}
else
{
throw new UserException(UserException.KErrGeneral,"Application not found");
}

}

catch(ActivityNotFoundException e)
{
e.printStackTrace();
throw new UserException(UserException.KErrGsmRRNoActivityOnRadioPath,e.getMessage());
}


catch(Exception e)
{
e.printStackTrace();
throw new UserException(UserException.KErrGeneral,e.getMessage());

}
return true;
}


keep smiling :)
ranjit0046
Developer
Developer
 
Posts: 47
Joined: Mon Dec 14, 2009 12:46 pm

Postby neilb » Wed Mar 24, 2010 9:57 am

neilb
Freshman
Freshman
 
Posts: 3
Joined: Wed Mar 10, 2010 10:07 am

Top

Return to SDK/ADT/Emulator Problems

Who is online

Users browsing this forum: No registered users and 5 guests