Android explicit intent with target component

后端 未结 5 1417
鱼传尺愫
鱼传尺愫 2020-12-09 03:45

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 ru

相关标签:
5条回答
  • 2020-12-09 04:22

    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.

    0 讨论(0)
  • 2020-12-09 04:36

    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.

    0 讨论(0)
  • 2020-12-09 04:37

    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);
    
    0 讨论(0)
  • 2020-12-09 04:44

    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);
    
    0 讨论(0)
  • 2020-12-09 04:45
    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);
    
    0 讨论(0)
提交回复
热议问题