Android 中的隐式与显式意图( startActivity(intent) 崩溃)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21647663/
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
Implicit vs Explicit Intent in Android ( startActivity(intent) crashes )
提问by coderplus
I'm new in this world. I have a problem when I use startActivity(intent). This is the Manifest:
我是这个世界的新手。我在使用 startActivity(intent) 时遇到问题。这是清单:
<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
我想显示 Splash 活动 5 秒钟,然后显示 MainActivity。日志错误:!https://www.dropbox.com/s/kg7xyp6h4b95itq/Screenshot%202014-02-08%2016.57.36.png
回答by coderplus
There are two ways of doing what you are trying to do.
有两种方法可以做你想做的事情。
- Using an implicit
Intent
- Using an explicit
Intent
- 使用隐式
Intent
- 使用显式
Intent
Refer Intent Types
参考意图类型
- Implicit
Intent
- 隐式
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.
声明Intent Filters
你Activity
在你的AndroidManifest.xml
。通过这样做,Android
系统了解Intents
您的组件(在本例中是您的 MainActivity)可以处理的类型。
<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
现在您将能够以Activity
相同的方式启动您的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.
Intents
当您不明确知道必须启动哪个 Activity 并且您希望 Android 系统决定启动哪个组件时,将使用这种隐式。如果系统找到多个可以处理您的 Intent 的组件,它将允许用户进行选择。
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.
注意:可能没有应用程序可以处理您的意图。在这种情况下,当您调用 startActivity() 时,您的应用程序将崩溃。为了避免这种情况,在调用 startActivity() 之前,您应该首先验证系统中是否至少注册了一个可以处理意图的应用程序。为此,请在您的意图对象上使用 resolveActivity()。
- Explicit
Intent
- 显式
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
因为您已经知道要从哪个Activity
开始。因此,Intent
通过传递上下文和要启动的组件(活动)类来创建一个。
Intent i=new Intent (this,MainActivity.class);
startActivity(i);
回答by nKn
You have to reference the class
you want to start. So you'd need something like:
你必须参考class
你想要开始的。所以你需要这样的东西:
Intent newAct = new Intent(this, Splash.class);
startActivity(newAct);
What you're passing is an Action
that is not understood as a class name.
你传递的是一个Action
不被理解为类名的。
回答by Chintan Soni
I guess, Splash
is your Launcher Activity, make following changes in your manifest file:
我想,Splash
是您的启动器活动,在您的清单文件中进行以下更改:
<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.
我希望这现在可以解决您的问题。
回答by Ali
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 filter
of MainActivity from:
但你需要改变action
在intent filter
MainActivity从:
<action android:name="android.intent.action.MAIN" />
to:
到:
<action android:name="com.example.counter.MainActivity"/>
回答by MysticMagic?
You need to declare an activityin 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.
希望能帮助到你。
回答by la martir martir
i think it is better if you use Handler put this code at the splash Activity at the onCreate
我认为如果您使用 Handler 将此代码放在 onCreate 的启动 Activity 中会更好
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
如果您想一次打开它,最好使用 SharedPreferences