Android的意图语法 [英] Android intent syntax

查看:240
本文介绍了Android的意图语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我试图找出如何开始在我的应用程序一个新的意图,我遇到措辞它的几种方法。

In my attempts to find out how to start a new intent in my app, I've come across several ways of phrasing it.

此语法返回运行时错误,即ActivityNotFound异常

This syntax returns a runtime error, namely a ActivityNotFound exception

Intent in = new Intent("com.something.something"); 

当然,我的Andr​​oid清单包含意图过滤器中的一个动作:

Of course my android manifest contains an action within the intent filter:

<activity
        android:name=".SecondActivity"
        android:label="@string/title_activity_second" >
        <intent-filter>
            <action android:name="com.something.something" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity> 

该格式如下:

Intent in = new Intent(MainActivity.this, SecondActivity.class); 

我也试过如下:

I also tried the following:

Intent in = new Intent(this, SomeActivity.class); 

这是被推荐的一本书我读。这将返回一个运行时错误,activitynotfound

that was recommended in a book I'm reading. This returns a runtime error, activitynotfound

这人让Eclipse的把我来回setClass和setClassName无限的:

This one makes Eclipse throw me back and forth between setClass and setClassName infinitely:

 Intent in = new Intent().setClass(this, SecondActivity.class);

我使用它的一个onclick方法:

I'm using it in an onclick method:

ok.setOnClickListener(new View.OnClickListener()
    {

        @Override
        public void onClick(View v)
        {

            Intent in = new Intent(MainActivity.this, SecondActivity.class);
            startActivity(in);
        }
    });
}

什么是它们之间的区别,为什么只是他们的工作对我来说一个?

What's the difference between these and why is only one of them working for me?

问候 / M

推荐答案

有关意向分辨率基本信息

意图可以包含以下基本信息:

Intents can contain the following basic information:

  • 操作
  • 类别
  • 数据
  • 组件

有2种方式的意图是由系统解析:

There are 2 ways that Intents are resolved by the system:

  • 在显式(使用组件如果指定的话)
  • 隐式(使用Action,类别和数据,以寻找合适的活动)

如果您指定那么这是用来明确地找到您所指定的活性成分(包名和类名),其目的是发送到该活动。其它意图数据不被使用(虽然它被传递到被叫活性在Intent)。这就是所谓的明确意图的决议

If you specify the component (package name and class name) then this is used to explicitly find the activity you have specified and the Intent is sent to that activity. The other Intent data is not used (although it is passed to the called activity in the Intent). This is called "explicit Intent resolution".

如果您不指定组件,则动作,类别和数据字段用于定位广告(通过意图过滤器),一个或多个活动,他们可以接受意向。这就是所谓的隐含的意图的决议

If you don't specify the component, then the ACTION, CATEGORY and DATA fields are used to locate one or more activities that advertise (via intent-filter) that they can accept the Intent. This is called "implicit Intent resolution".

您的具体问题

当你这样做:

Intent in = new Intent("com.something.something");

您正在创建一个意图和设置操作为com.something.something。如果你再调用 startActivity()这个意图,你得到 ActivityNotFoundException 由于Android无法找到一个活动,可以接受意图用行动=com.something.something。究其原因是因为你提供了一个意向过滤用行动=com.something.something类和=android.intent.category.LAUNCHER,但你有没有指定的类别在你的意图(机器人会自动添加类别默认一个意向,如果没有在使用指定的任何类别 startActivity())。为了使这项工作就应该把

You are creating an implicit Intent and setting the ACTION to "com.something.something". If you then call startActivity() with this Intent, you get ActivityNotFoundException because Android cannot find an activity that can accept an Intent with ACTION="com.something.something". The reason is because you have provided an intent-filter with ACTION="com.something.something" and CATEGORY="android.intent.category.LAUNCHER" but you haven't specified the CATEGORY in your Intent (Android automatically adds the CATEGORY "DEFAULT" to an Intent if there isn't any CATEGORY specified when using startActivity()). To make this work you should either

  • 替换类别=android.intent.category.LAUNCHER类别=android.intent.category.DEFAULT
  • 添加&LT;类机器人:名称=android.intent.category.DEFAULT/&GT;
  • Replace CATEGORY="android.intent.category.LAUNCHER" with CATEGORY="android.intent.category.DEFAULT" or
  • Add <category android:name="android.intent.category.DEFAULT" />

到意向过滤器 SecondActivity

当你这样做:

Intent in = new Intent(MainActivity.this, SecondActivity.class);

您正在创建一个明确意图指定组件 SecondActivity 。此方法的签名是意图(上下文packageContext,类CLAS)。它使用 packageContext 包名称和 CLAS 类名来创建一个明确的意向,该组件。如果使用此构造一个活动里,你可以使用作为第一个参数,因为活动扩展上下文。如果你从另一个类使用此构造函数(像 OnClickListener ),你需要指定 MyActivity.this 作为第一个参数传递活动不和OnClickListener实例(因为 OnClickListener 不延上下文

You are creating an explicit Intent specifying the component SecondActivity. The signature for this method is Intent(Context packageContext, Class clas). It uses the package name from packageContext and the class name from clas to create an explicit Intent for that component. If you use this constructor inside an Activity, you can just use this as the first parameter, because Activity extends Context. If you use this constructor from another class (like an OnClickListener) you need to specify MyActivity.this as the first parameter to pass the instance of the Activity and not of the OnClickListener (because OnClickListener does not extend Context).

当你这样做:

Intent in = new Intent().setClass(this, SecondActivity.class);

您正在创建一个明确意图如上。这是完全一样的使用:

you are creating an explicit Intent as above. This is exactly the same as using:

Intent in = new Intent(this, SecondActivity.class);

您不能内的 OnClickListener 因为第一个参数需要一个上下文(或做扩展上下文,像活动)。

You can't do this inside an OnClickListener because the first parameter needs to be a Context (or a class that extends Context, like Activity).

如果你想创建一个明确意图,你也可以使用这样的:

If you want to create an explicit Intent you can also use this:

Intent in = new Intent().setClassName("com.something", "com.something.SecondActivity");

这将创建一个明确的意图,但你并不需要一个上下文这一点。你可以只通过包名和类名作为字符串(如果你知道它们)。

This creates an explicit intent, but you don't need a Context for this. You can just pass the package name and the class name as Strings (if you know them).

有关意向分辨率的详细信息,请参见:

For more information about Intent resolution, see:

  • <一个href="http://developer.android.com/guide/components/intents-filters.html">http://developer.android.com/guide/components/intents-filters.html
  • <一个href="http://developer.android.com/reference/android/content/Intent.html">http://developer.android.com/reference/android/content/Intent.html
  • <一个href="http://developer.android.com/reference/android/content/IntentFilter.html">http://developer.android.com/reference/android/content/IntentFilter.html
  • http://developer.android.com/guide/components/intents-filters.html
  • http://developer.android.com/reference/android/content/Intent.html
  • http://developer.android.com/reference/android/content/IntentFilter.html

这篇关于Android的意图语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆