Android中的隐式vs显式意图(startActivity(intent)crashes)

Android中的隐式vs显式意图(startActivity(intent)crashes)

问题描述:

我是这个世界的新手.使用startActivity(intent)时出现问题. 这是清单:

I'm new in this world. I have a problem when I use startActivity(intent). This is the Manifest:

<activity
        android:name="com.example.counter.Splash"
        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="com.example.counter.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

这是代码:

 public class Splash extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.splash);

    Thread timer = new Thread(){
        public void run()
        {
            try
            {
                sleep(5000);

            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            finally
            {

                Intent i=new Intent ("com.example.counter.MainActivity");
                startActivity(i);
            }
        }
    };

    timer.start();

}

我想显示Splash活动5秒钟,然后显示MainActivity. LogErrors:! https://www.dropbox .com/s/kg7xyp6h4b95itq/Screenshot%202014-02-08%2016.57.36.png

I'd want to show Splash activity for 5 seconds and then show MainActivity. LogErrors: !https://www.dropbox.com/s/kg7xyp6h4b95itq/Screenshot%202014-02-08%2016.57.36.png

有两种方法可以完成您想做的事情.

There are two ways of doing what you are trying to do.

  1. 使用隐式Intent
  2. 使用明确的Intent
  1. Using an implicit Intent
  2. Using an explicit Intent

引用意图类型

  1. 隐式Intent

AndroidManifest.xml中为Activity声明Intent Filters.这样,Android系统就可以了解您的组件(在本例中为MainActivity)可以处理哪种Intents.

Declare Intent Filters for your Activity in your AndroidManifest.xml. By doing that the Android system understands what kind of Intents your component(in this case your MainActivity) can handle.

<activity
        android:name="com.example.counter.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.counter.MainAction" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
<activity>

现在,您将可以使用相同的Intent

Now you will be able to launch your Activity with the same Intent

Intent i=new Intent ("com.example.counter.MainAction");
startActivity(i);

当您不明确知道必须启动哪个Activity并希望Android系统决定要启动哪个组件时,将使用此类隐式Intents.如果系统找到可以处理您的Intent的多个组件,它将允许用户进行选择.

Such implicit Intents are used when you don't explicitly know which Activity has to be started and you want the Android system to decide which component to start. If the system finds multiple components which can handle your Intent, it will allow the user to choose.

注意:可能没有任何应用程序可以处理您的意图.在这种情况下,调用startActivity()时,应用程序将崩溃.为避免这种情况,在调用startActivity()之前,您首先应验证系统中至少有一个可以处理该意图的应用程序.为此,请在您的意图对象上使用resolveActivity().

Note: it is possible that there are no applications that can handle your intent. In this case, your application will crash when you invoke startActivity(). To avoid this, before calling startActivity() you should first verify that there is at least one application registered in the system that can handle the intent. To do this use resolveActivity() on your intent object.

  1. 显式Intent

对于您的情况,应该使用显式的Intent,因为您已经知道要启动哪个Activity.因此,通过传递上下文和要启动的component(Activity)类来创建Intent.

In your case, you should use an explicit Intent as you already know which Activity you want to start. So create an Intent by passing the context and the component(Activity) class you want to start.

Intent i=new Intent (this,MainActivity.class);
startActivity(i);