19

Is it possible to fire explicit intent but not for an activity from my project but for activity in some other application.

I am sure of this code and I know it is running

Intent i=new Intent(this,MyActivity.class);

But is it possible to do something like this

Intent i=new Intent(this,com.bzz.bla.bla.SomeActivity.class);

I mean what is the exact way of explicitly starting activity from other application (activity that is contained in other apk), is this possible at all ?

I tried but it drops me force close message.

Tom Taylor
  • 2,378
  • 1
  • 27
  • 48
Lukap
  • 29,596
  • 60
  • 146
  • 239
  • Yes it is possible,You can call another Activity of another package like this. See [details](https://stackoverflow.com/questions/2780102/open-another-application-from-your-own-intent) here – Rasel Jul 26 '11 at 11:19

5 Answers5

39

Yes it's possible. But creation of intent is different.Try this:

Intent intent = new Intent();
intent.setComponent(new ComponentName("The package name of the activity that you wish to launch","Its fully qualified class name"));
startActivityForResult(intent);
Sagar
  • 2,480
  • 2
  • 24
  • 34
SRam
  • 2,704
  • 4
  • 41
  • 70
  • 8
    classname.java? Does that work? Shouldn't it be the fully qualified name of the class (that has no extension) like this: com.bzz.bla.bla.SomeActivity – Jose_GD Mar 17 '14 at 14:02
  • 1
    It doesn't! I actually tried to add .java or .class to a working app in order to test it, and it starts to throw ActivityNotFoundException exception. This answer is misleading and can waste lots of time for learners. – Tankman六四 Jan 05 '17 at 05:59
  • @ZhangWeiwu Read the question first then my reply and your requirement.. i think you need to requre to add new .java/ class file to your project so as per my understanding according to your comment you need to declare that .java file to your manifest file also for avoiding "ActivityNotFoundException " exception, hope this works for you...and expect UPVOTE for this.... – SRam Jan 05 '17 at 06:21
  • 1
    here is where the confusion was from. In my comment - "add .java" - I was seconding @Jose_GD (adding ".java" to the 2nd parameter), not meaning adding .java file. I have a project which is already working, where the second parameter of ComponentName is a fully qualified class name in quotes. So I wasn't seeking a solution to make it work. Seeing your post I tested adding ".java" to the end of the fully qualified class name and broke it. You can remove ".java" in your answer to make it correct and I'll then upvote! – Tankman六四 Jan 06 '17 at 21:52
7

Yes it's possible. But the intent creation is a little different.

Intent intent = new Intent();
intent.setComponent(new ComponentName("The package name of the activity that you wish to launch","Its fully qualified class name"));
startActivity(intent);

But, then you just can't call any activity of any random app. That particular activity should have an intent-filter with a MAIN action.

Sagar
  • 2,480
  • 2
  • 24
  • 34
Kumar Bibek
  • 8,851
  • 2
  • 36
  • 64
  • Is it possible to avoid use of the MAIN intent filter? Eg: If the calling and the called applications are both signed by the same keystore? – reubenjohn Aug 14 '15 at 07:19
3

You can start any component through intent only need to know either action or target component (pkg,cls) name.
Consider I developed two apps app1 & app2 app1 pkg name is com.xyz.app1 & app2 pkg name is com.xyz.app2.

app1 having two activities App1MainActivity & App1XyzActivity ,app2 has only one activity App2MainActivity now I want to start both the activity of app1 from app2 App2MainActivity
app2 App2MainActivity have two buttons b1 & b2 on click b1 I want to start App1MainActivity & on click b2 I want to start App1XyzActivity then the code for button b1 and b2 within App2MainActivity is

b1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {


        Intent i = new Intent();
        String pkg = "com.xyz.app1";
        String cls = "com.xyz.app1.App1MainActivity";
        i.setComponent(new ComponentName(pkg, cls));
        App2MainActivity.this.startActivity(i);

    }
});

b2.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {


        Intent i = new Intent();
        String pkg = "com.xyz.app1";
        String cls = "com.xyz.app1.App1XyzActivity";
        i.setComponent(new ComponentName(pkg, cls));
        App2MainActivity.this.startActivity(i);

    }
});

now I install both apps app1 & app2 and run the app2.
When I click on the button b1 then app1 App1MainActivity is start but if I click on button b2 an exception occur the reason is we can not start randomly any activity of another app even if you know the package name and its class name but you can start another app main activity if it have intent filter with action MAIN and if you know its package name and class name.

benka
  • 4,662
  • 35
  • 44
  • 58
S.H.KHAN
  • 31
  • 3
1

I'd suggest to concatenate the package & class name with a dot;

this speeds up copy & paste, eg. while writing jUnit tests.

String packageName = getApplicationContext().getPackageName();
String className = "SomeActivity";

Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, packageName + "." + className));
startActivity(intent);
Martin Zeitler
  • 49,224
  • 12
  • 97
  • 156
0
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("np.birthday.com.order", "np.birthday.com.order.MainActivity");// intent.setClassName("Package NAme of another application", "np.birthday.com.order.MainActivity");
startActivity(intent);
OM Tech
  • 21
  • 1
  • 3