Android 与目标组件的显式意图

发布于 2024-11-26 12:27:45 字数 335 浏览 1 评论 0原文

是否可以激发明确的意图,但不是针对我的项目中的活动,而是针对其他应用程序中的活动。

我确信这段代码并且我知道它正在运行

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

但是是否可以做这样的事情

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

我的意思是从其他应用程序显式启动活动的确切方法是什么(其他apk中包含的活动), 这有可能吗?

我尝试过,但它让我强制关闭消息。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

掐死时间 2024-12-03 12:27:46

是的,这是可能的。但意图的创建是不同的。试试这个:

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);

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);
夜巴黎 2024-12-03 12:27:46

是的,这是可能的。但意图创建有点不同。

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);

但是,您就无法调用任何随机应用程序的任何活动。该特定活动应该有一个带有 MAIN 操作的意图过滤器。

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.

月寒剑心 2024-12-03 12:27:46

您可以通过意图启动任何组件,只需要知道操作或目标组件(pkg,cls)名称即可。
考虑我开发了两个应用程序 app1 & app2 app1 pkg 名称为 com.xyz.app1 & app2 pkg 名称为 com.xyz.app2.

app1 有两个活动 App1MainActivity & App1XyzActivityapp2 只有一个 Activity App2MainActivity 现在我想从 启动 app1 的 Activity app2 App2MainActivity
app2 App2MainActivity 有两个按钮 b1 和 b1 b2 单击 b1 我想启动 App1MainActivity &单击 b2 我想启动 App1XyzActivity 然后 App2MainActivity 中按钮 b1 和 b2 的代码现在是

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);

    }
});

我安装了两个应用程序 app1 和 app1 app2 并运行 app2。
当我单击按钮 b1 时,app1 App1MainActivity 启动,但如果我单击按钮 b2 则会发生异常,原因是我们无法随机启动另一个应用程序的任何活动,即使您知道包名称及其类名称,但您可以启动另一个应用程序的主要活动,如果它有带有操作 MAIN 的意图过滤器,并且您知道它的包名称和类名称。

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.

瑶笙 2024-12-03 12:27:46

我建议连接包 &带点的类名;

这可以加快复制速度粘贴,例如。在编写 jUnit 测试时。

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

Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, packageName + "." + className));
startActivity(intent);

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);
时光无声 2024-12-03 12:27:46
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);
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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文