Android 中的意图过滤器是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3321514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are intent-filters in Android?
提问by ankitjaininfo
In my android app, I wanted to start an activity 'B' from initial activity 'A'. I have created classes for both of these. However when using following code to start B, I get a runtime error: application has stopped unexpectedly, try again
. Here is my code:
在我的 android 应用程序中,我想从初始活动“A”开始一个活动“B”。我已经为这两个类创建了类。然而,使用下面的代码来启动B时的,我得到一个运行时错误:application has stopped unexpectedly, try again
。这是我的代码:
Intent myIntent = new Intent(this, AddNewActivity.class);
startActivity(myIntent);
When I added a new entry in AndroidManifest.xml/manifest/application/activity/intent-filers
for activity B then the application worked.
当我AndroidManifest.xml/manifest/application/activity/intent-filers
为活动 B添加一个新条目时,应用程序就工作了。
I have two questions:
我有两个问题:
- When there are multiple activities entries in
AndroidManifest.xml
, how does android know which activity to start first? - I could not understand intent-filters. Can anyone please explain.
- 当 中有多个活动条目时
AndroidManifest.xml
,android如何知道首先启动哪个活动? - 我无法理解意图过滤器。任何人都可以请解释。
Here is my partial AndroidManifest.xml
这是我的部分 AndroidManifest.xml
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ListAllActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AddNewActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
采纳答案by CommonsWare
When there are multiple activities entries in AndroidManifest.xml, how does android know which activity to start first?
当AndroidManifest.xml中有多个activity条目时,android如何知道哪个activity先启动?
There is no "first". In your case, with your manifest as shown, you will have two icons in your launcher. Whichever one the user taps on is the one that gets launched.
没有“第一”。在你的例子中,你的清单如图所示,你的启动器中有两个图标。用户点击哪一个就是启动的那个。
I could not understand intent-filters. Can anyone please explain.
我无法理解意图过滤器。任何人都可以请解释。
There is quite a bit of documentation on the subject. Please consider reading that, then asking more specific questions.
有很多关于这个主题的文档。请考虑阅读该内容,然后提出更具体的问题。
Also, when you get "application has stopped unexpectedly, try again", use adb logcat
, DDMS, or the DDMS perspective in Eclipse to examine the Java stack trace associated with the error.
此外,当您收到“应用程序意外停止,再试一次”时,请使用adb logcat
、DDMS 或 Eclipse 中的 DDMS 透视图来检查与错误关联的 Java 堆栈跟踪。
回答by santoshpatmca
An intent filter is an expression in an app's manifest file that specifies the type of intents that the component would like to receive.
Intent 过滤器是应用程序清单文件中的一个表达式,用于指定组件希望接收的 Intent 类型。
When you create an implicit intent, the Android system finds the appropriate component to start by comparing the contents of the intent to the intent filters declared in the manifest file of other apps on the device. If the intent matches an intent filter, the system starts that component and delivers it the Intent object.
当您创建一个隐式 Intent 时,Android 系统会通过将 Intent 的内容与设备上其他应用程序的清单文件中声明的 Intent 过滤器进行比较来找到合适的组件来启动。如果意图与意图过滤器匹配,系统将启动该组件并将其传递给意图对象。
AndroidManifest.xml
AndroidManifest.xml
<activity android:name=".HelloWorld"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="http" android:host="androidium.org"/>
</intent-filter>
</activity>
Launch HelloWorld
启动HelloWorld
Intent intent = new Intent (Intent.ACTION_VIEW, Uri.parse("http://androidium.org"));
startActivity(intent);
回答by Mahadevappa T
The Activity
which you want it to be the very first screen if your app is opened, then mention it as LAUNCHER in the intent category and remaining activities mention Default in intent category.
在Activity
你希望它是最先屏幕,如果你的应用程序被打开,然后提起它作为LAUNCHER在意向类别和剩余活动提意向类别默认。
For example:- There is 2 activity A and B
The activity A is LAUNCHER so make it as LAUNCHER in the intent Category and B is child for Activity A so make it as DEFAULT.
例如:- 有 2 个活动 A 和 B
活动 A 是 LAUNCHER,因此在意图类别中将其设置为 LAUNCHER,而 B 是活动 A 的子项,因此将其设置为 DEFAULT。
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ListAllActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AddNewActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
回答by Michael Horojanski
When you create an implicit intent, the Android system finds the appropriate component to start by comparing the contents of the intent to the intent filters declared in the manifest file of other apps on the device. If the intent matches an intent filter, the system starts that component and delivers it the Intent object. If multiple intent filters are compatible, the system displays a dialog so the user can pick which app to use.
当您创建一个隐式 Intent 时,Android 系统会通过将 Intent 的内容与设备上其他应用程序的清单文件中声明的 Intent 过滤器进行比较来找到合适的组件来启动。如果意图与意图过滤器匹配,系统将启动该组件并将其传递给意图对象。如果多个意图过滤器兼容,系统会显示一个对话框,以便用户可以选择要使用的应用程序。
An intent filter is an expression in an app's manifest file that specifies the type of intents that the component would like to receive. For instance, by declaring an intent filter for an activity, you make it possible for other apps to directly start your activity with a certain kind of intent. Likewise, if you do not declare any intent filters for an activity, then it can be started only with an explicit intent.
Intent 过滤器是应用程序清单文件中的一个表达式,用于指定组件希望接收的 Intent 类型。例如,通过为 Activity 声明一个 Intent 过滤器,您可以让其他应用程序直接使用某种 Intent 启动您的 Activity。同样,如果您没有为活动声明任何意图过滤器,则它只能以显式意图启动。
According: Intents and Intent Filters
根据:意图和意图过滤器
回答by Excel Logix
First change the xml, mark your second activity as DEFAULT
首先更改xml,将您的第二个活动标记为DEFAULT
<activity android:name=".AddNewActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Now you can initiate this activity using StartActivity method.
现在您可以使用 StartActivity 方法启动此活动。
回答by Bajirang namade
intent filter is expression which present in manifest in your app that specify the type of intents that the component is to receive. If component does not have any intent filter it can receive explicit intent. If component with filter then receive both implicit and explicit intent
意图过滤器是出现在您的应用程序清单中的表达式,用于指定组件要接收的意图类型。如果组件没有任何意图过滤器,它可以接收明确的意图。如果组件带有过滤器,则同时接收隐式和显式意图
回答by Prashant
Keep the first intent filter with keys MAIN
and LAUNCHER
and add another as ANY_NAME
and DEFAULT
.
使用键MAIN
和保留第一个意图过滤器LAUNCHER
并添加另一个作为ANY_NAME
和DEFAULT
。
Your LAUNCHER
will be activity A and DEFAULT
will be your activity B.
您LAUNCHER
将成为活动 A,DEFAULT
将成为您的活动 B。
回答by Saikat Biswas
There can be no two Lancher AFAIK. Logcat is a usefull tool to debug and check application/machine status in the behind. it will be automatic while switching from one activity to another activity.
不可能有两个 Lancher AFAIK。Logcat 是一个有用的工具,用于在后台调试和检查应用程序/机器状态。从一项活动切换到另一项活动时,它将是自动的。
回答by Shiva-mamaa
If possible try this one instant solution:
如果可能,请尝试这个即时解决方案:
Intent intent =new Intent(getApplicationBaseContext,second_Act.class);
StartActivity(intent);
回答by Anand Saggam
When there are multiple activities set as main and launcher with an intent filter in the manifest. Then first activity considers as Launcher activity, and android launch or open the first activity.
当有多个活动设置为 main 和启动器时,清单中有一个意图过滤器。然后第一个活动被视为启动器活动,并且android启动或打开第一个活动。
<category android:name="android.intent.category.LAUNCHER" />
The above code makes an app icon available in the device menu, so if we declare 2 launcher activity in the manifest, there will be 2 app icons get created in the device app menu.
上面的代码使设备菜单中的应用程序图标可用,因此如果我们在清单中声明 2 个启动器活动,将在设备应用程序菜单中创建 2 个应用程序图标。
So there will be 2 app icons, on click of the first icon, first declared activity in manifest will be launch, and click of another second declared activity gets launch
所以会有 2 个应用程序图标,点击第一个图标,清单中第一个声明的活动将被启动,然后点击另一个第二个声明的活动被启动