Implicit vs Explicit Intent in Android ( startActi

2020-02-26 11:03发布

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>

And this is the code:

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

}

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

6条回答
霸刀☆藐视天下
2楼-- · 2020-02-26 11:40

i think it is better if you use Handler put this code at the splash Activity at the onCreate

new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
                finish();
            }
        }, 1500); 

and if you want to open it one time it is good to use SharedPreferences

查看更多
smile是对你的礼貌
3楼-- · 2020-02-26 11:53

I guess, Splash is your Launcher Activity, make following changes in your manifest file:

<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" >
</activity>

Make your activity this way:

public class Splash extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        /*Splach screen that display for 5 seconds when app start*/
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent i = new Intent(Splash.this, MainActivity.class);
                startActivity(i);
                finish();
            }
        }, 5000);

    }

}

I hope this should solve your problem now.

查看更多
Ridiculous、
4楼-- · 2020-02-26 11:54

You need to declare an activity in manifest file.

Like this:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".FirstActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activty
        android:name="com.example.counter.MainActivity"/>
</application>

Hope it helps.

查看更多
何必那么认真
5楼-- · 2020-02-26 11:55

I think you should be able to use (implicit Intent):

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

There is no reason to change it to (explicit intent):

startActivity(new Intent(mContext, MainActivity.class));

but then you need to change the action in intent filterof MainActivity from:

<action android:name="android.intent.action.MAIN" />

to:

<action android:name="com.example.counter.MainActivity"/>
查看更多
Fickle 薄情
6楼-- · 2020-02-26 11:59

You have to reference the class you want to start. So you'd need something like:

Intent newAct = new Intent(this, Splash.class);
startActivity(newAct);

What you're passing is an Action that is not understood as a class name.

查看更多
Summer. ? 凉城
7楼-- · 2020-02-26 12:06

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

  1. Using an implicit Intent
  2. Using an explicit Intent

Refer Intent Types

  1. Implicit Intent

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>

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

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

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.

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. Explicit 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);
查看更多
登录 后发表回答