简体   繁体   English

如何使用广播从意图开始一项活动?

[英]How to start an activity from intent using broadcast?

I am using the code to call an intent in Android 我正在使用代码在Android中调用意图

Intent intent = new Intent();
String PACKAGE_NAME="com...."
intent.setPackage(PACKAGE_NAME);
intent.setAction(Intent.ACTION_VOICE_COMMAND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
getApplication().startActivity(intent);

Unfortunately, in some case, I do not know the PACKAGE_NAME. 不幸的是,在某些情况下,我不知道PACKAGE_NAME。 So, another method is using broadcast. 因此,另一种方法是使用广播。 How can I use it? 如何使用? Thanks all 谢谢大家

You cant call Activity directly if you dont know package and name of that Activity. 如果您不知道该活动的软件包名称 ,则无法直接致电该活动。

Since you dont know package and Activity name you can only ask OS to show all possible variants for your Intent Action. 由于您不知道程序包和活动名称,因此只能要求 OS显示您的Intent Action的所有可能变体。 In most cases user click "always open this app for this action" what means that you will directly open another app. 在大多数情况下,用户单击“始终为此操作打开此应用程序”,这意味着您将直接打开另一个应用程序。

So in your case your code should looks like 因此,在您的情况下,您的代码应类似于

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VOICE_COMMAND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//dont forget to check if user has at least one application for your Intent Action
if (intent.resolveActivity(getPackageManager()) != null) {
    context.startActivity(intent);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM