0

Ok, so what I want to do is create a widget that will simply launch another application when the widget is pressed. The applications, however, are not ones I have created, i.e. the Market app, the Browser and things like that. I have already setup the AppWidgetProvider and did all the changing in the manifest and all those things. I just need to know what I must add to the class/Java Source File that will do so. Any help is appreciated, thanks!

natehoch96
  • 69
  • 4
  • 8
  • possible duplicate of [How to launch activity from android home screen widget](http://stackoverflow.com/questions/2706464/how-to-launch-activity-from-android-home-screen-widget) – citizen conn Jul 25 '11 at 22:15

1 Answers1

2

Try doing this:

String packageName = "com.package";
String className = "com.package.MainActivity";
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName(packageName, className));
startActivity(intent);

UPDATE:

This looks like a better way of launching an application:

PackageManager pm = getPackageManager();
try
{
    String packageName = "com.example.package";
    Intent launchIntent = pm.getLaunchIntentForPackage(packageName);
    startActivity(launchIntent);
}
catch (Exception e1)
{
}
A. Abiri
  • 10,750
  • 4
  • 30
  • 31
  • I get a few errors but the main ones seem to be that "the return type is missing for startActivity" and that "intent cannot be resolved to a type" I am very new at developing so please mind my "noobishness," thanks for the help. – natehoch96 Jul 26 '11 at 02:08
  • Make sure that you have the correct package name and class name. Also, try changing startActivity(intent) to MyActivity.this.startActivity(intent). If that doesn't work, I would need you to show me how the code looks like when you put your values in because I have put this code in my application and it works without any problems. – A. Abiri Jul 26 '11 at 05:06
  • Ok, i thought I had it working, but now its giving me an error at "intent.setComponent" saying "Syntax error on token "setComponent", = expected after this token." What should I do? Thanks. – natehoch96 Jul 28 '11 at 19:04
  • Can you show me your code, with the packagename and the classname, so I can tell you why you are getting the error? – A. Abiri Jul 28 '11 at 20:41
  • I just found another way to launch an application which seems a lot cleaner and official. It worked in an activity class, but I have not tested it in a service. I put the code in my answer in the updated section. See if that works. If it doesn't then read my previous comment. – A. Abiri Jul 28 '11 at 20:51
  • Ok, the new code seems to work for me. Although, I now have ran into more errors, but luckily these errors are unrelated to the code you provided. I started a new post about it however since I didnt want to clutter this one with unrelated questions. Thank you for all your help. – natehoch96 Jul 29 '11 at 01:08
  • Wow. He's thankful, but didn't upvote or accept your answer. Weak sauce. This worked perfect for me. +1 – xdumaine Dec 07 '12 at 18:22