Android开发学习笔记:浅谈显示Intent和隐式Intent
2017-08-04 10:21
507 查看
Intent寻找目标组件的两种方式:
显式Intent:通过指定Intent组件名称来实现的,它一般用在知道目标组件名称的前提下,一般是在相同的应用程序内部实现的。
隐式Intent:通过Intent Filter来实现的,它一般用在没有明确指出目标组件名称的前提下,一般是用于在不同应用程序之间。
一.显式Intent
一般情况下,一个Android应用程序中需要多个屏幕,即是多个Activity类,并且在这些Activity之间进行切换通过Intent机制来实现的。在同一个应用程序中切换Activity时,我们通常都知道要启动的Activity具体是哪一个,因此常用显式的Intent来实现的。
下面的例子是在同一应用程序中MainActivity启动SecondActivity,下面的代码中,主要是为“转到SecondActivity”按钮添加了OnClickListener,使得按钮被点击时执行onClick()方法,onClick()方法中则利用了Intent机制,来启动SecondActivity,关键的代码是22~25行。
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello1"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="转到SecondActivity"
/>
</LinearLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello2"
/>
<Button
android:id="@+id/secondBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回"
/>
</LinearLayout>
MainActivity.java
package com.android.test.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.btn);
//响应按钮btn事件
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//显示方式声明Intent,直接启动SecondActivity
Intent it = new Intent(MainActivity.this,SecondActivity.class);
//启动Activity
startActivity(it);
}
});
}
}
SecondActivity.java
package com.android.test.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SecondActivity extends Activity {
private Button secondBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
secondBtn=(Button)findViewById(R.id.secondBtn);
//响应按钮secondBtn事件
secondBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//显示方式声明Intent,直接启动MainActivity
Intent intent = new Intent(SecondActivity.this,MainActivity.class);
//启动Activity
startActivity(intent);
}
});
}
}
AndroidManifest.xml清单文件,16~18行为SecondActivity在清单文件里的声明
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.test.activity"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
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=".SecondActivity"
android:label="@string/app_name">
</activity>
</application>
</manifest>
效果图:
二.隐式Intent
下面是同一应用程序中的Activity切换的例子,需要AndroidManifest.xml中增加Activity的声明,并设置对应的Intent
Filter和Action,才能被Android的应用程序框架所匹配。
MainActivity.java
package com.android.change.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btn);
// 响应按钮btn事件
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 实例化Intent
Intent it = new Intent();
//设置Intent的Action属性
it.setAction("com.android.activity.MY_ACTION");
// 启动Activity
startActivity(it);
}
});
}
}
SecondActivity.java
package com.android.change.activity;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="转到SecondActivity"
/>
</LinearLayout>
seond.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/second"
/>
</LinearLayout>
AndroidManifest.xml文件的18,19行修改了Intent
Filter,这样SecondActivity才能够接收到MainActivity发送的Intent。因为在MainActivity的Intent发送的动作为"com.android.activity.MY_ACTION",而在18行里,SecondActivity设置的Action也为"com.android.activity.MY_ACTION",这样就能进行匹配。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.change.activity"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
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=".SecondActivity" >
<intent-filter>
<action android:name = "com.android.activity.MY_ACTION" />
<category android:name = "android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
效果图:
对于显示Intent,Android不需要再去做解析,因为目标组件很明确。Android需要解析的是隐式Intent,通过解析,将Intent映射给可以处理该Intent的Activity,Service等。Intent的解析机制主要是通过查找已经注册在AndroidManifest.xml中的所有IntentFilter以及其中定义的Intent,最终找到匹配的Intent。
本文章来自lingdududu 的BLOG http://liangruijun.blog.51cto.com/3061169/655132,再次谢谢分享
显式Intent:通过指定Intent组件名称来实现的,它一般用在知道目标组件名称的前提下,一般是在相同的应用程序内部实现的。
隐式Intent:通过Intent Filter来实现的,它一般用在没有明确指出目标组件名称的前提下,一般是用于在不同应用程序之间。
一.显式Intent
一般情况下,一个Android应用程序中需要多个屏幕,即是多个Activity类,并且在这些Activity之间进行切换通过Intent机制来实现的。在同一个应用程序中切换Activity时,我们通常都知道要启动的Activity具体是哪一个,因此常用显式的Intent来实现的。
下面的例子是在同一应用程序中MainActivity启动SecondActivity,下面的代码中,主要是为“转到SecondActivity”按钮添加了OnClickListener,使得按钮被点击时执行onClick()方法,onClick()方法中则利用了Intent机制,来启动SecondActivity,关键的代码是22~25行。
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello1"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="转到SecondActivity"
/>
</LinearLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello2"
/>
<Button
android:id="@+id/secondBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回"
/>
</LinearLayout>
MainActivity.java
package com.android.test.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.btn);
//响应按钮btn事件
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//显示方式声明Intent,直接启动SecondActivity
Intent it = new Intent(MainActivity.this,SecondActivity.class);
//启动Activity
startActivity(it);
}
});
}
}
SecondActivity.java
package com.android.test.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SecondActivity extends Activity {
private Button secondBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
secondBtn=(Button)findViewById(R.id.secondBtn);
//响应按钮secondBtn事件
secondBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//显示方式声明Intent,直接启动MainActivity
Intent intent = new Intent(SecondActivity.this,MainActivity.class);
//启动Activity
startActivity(intent);
}
});
}
}
AndroidManifest.xml清单文件,16~18行为SecondActivity在清单文件里的声明
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.test.activity"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
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=".SecondActivity"
android:label="@string/app_name">
</activity>
</application>
</manifest>
效果图:
二.隐式Intent
下面是同一应用程序中的Activity切换的例子,需要AndroidManifest.xml中增加Activity的声明,并设置对应的Intent
Filter和Action,才能被Android的应用程序框架所匹配。
MainActivity.java
package com.android.change.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btn);
// 响应按钮btn事件
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 实例化Intent
Intent it = new Intent();
//设置Intent的Action属性
it.setAction("com.android.activity.MY_ACTION");
// 启动Activity
startActivity(it);
}
});
}
}
SecondActivity.java
package com.android.change.activity;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="转到SecondActivity"
/>
</LinearLayout>
seond.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/second"
/>
</LinearLayout>
AndroidManifest.xml文件的18,19行修改了Intent
Filter,这样SecondActivity才能够接收到MainActivity发送的Intent。因为在MainActivity的Intent发送的动作为"com.android.activity.MY_ACTION",而在18行里,SecondActivity设置的Action也为"com.android.activity.MY_ACTION",这样就能进行匹配。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.change.activity"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
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=".SecondActivity" >
<intent-filter>
<action android:name = "com.android.activity.MY_ACTION" />
<category android:name = "android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
效果图:
对于显示Intent,Android不需要再去做解析,因为目标组件很明确。Android需要解析的是隐式Intent,通过解析,将Intent映射给可以处理该Intent的Activity,Service等。Intent的解析机制主要是通过查找已经注册在AndroidManifest.xml中的所有IntentFilter以及其中定义的Intent,最终找到匹配的Intent。
本文章来自lingdududu 的BLOG http://liangruijun.blog.51cto.com/3061169/655132,再次谢谢分享
相关文章推荐
- Android开发学习笔记:浅谈显示Intent和隐式Intent
- Android开发学习笔记:浅谈显示Intent和隐式Intent
- Android开发学习笔记:浅谈显示Intent和隐式Intent
- Android开发学习笔记:浅谈显示Intent和隐式Intent
- Android开发学习笔记:浅谈显示Intent和隐式Intent
- Android开发学习笔记(4):浅谈显示Intent和隐式Intent
- Android开发学习笔记:浅谈显示Intent和隐式Intent
- Android开发学习笔记:浅谈显示Intent和隐式Intent
- Android开发学习笔记:浅谈显示Intent和隐式Intent
- Android开发学习之浅谈显示Intent和隐式Intent
- Android开发学习笔记:浅谈WebView
- Android 开发学习笔记(二)—— Activity和Intent
- Android开发学习笔记之浅谈WebView
- Android开发学习笔记:浅谈WebView
- Android开发学习笔记:浅谈GridView
- Android开发学习笔记:浅谈WebView
- Android:Intent 显示和隐式 学习
- Android开发学习笔记:浅谈ToggleButton
- Android开发学习笔记:浅谈Content Provider
- Android深入探究笔记 -- Intent (隐式意图和显示意图)