android基础--意图的学习
来源:互联网 发布:淘宝首页流量入口 编辑:程序博客网 时间:2024/05/24 01:42
Main.xml
<LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="@string/button"/>
</LinearLayout>
Other.xml
<LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new ButtonClickListener());
}
private final class ButtonClickListener implements View.OnClickListener{
public void onClick(View v) {
Intent intent = new Intent();
//采用隐式意图激活OtherActivity,下面两个参数在Manifest中配置好了
intent.setAction("cn.com.me");//传入动作参数
//intent.addCategory("android.intent.category.DEFAULT");//传入类别参数
startActivity(intent);//方法内回味intent设置类别android.intent.category.DEFAULT上面的传入类别参数的代码不用要
}
}
}
otherActivity.java
public class OtherActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.com.intent"
android:versionCode="1"
android:versionName="1.0">
<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>
<!-- 完成对OtherActivity的配置 -->
<activity android:name=".OtherActivity" android:label="@string/otheractivity">
<!-- 配置意图过滤器android:name="cn.com.me"动作名称 -->
<intent-filter>
<!-- 动作参数 -->
<action android:name="cn.com.me"/>
<!-- 类别参数 -->
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
有数据参数时候的匹配情况举例:
<!-- 数据参数 -->
<data android:scheme="aaa" android:host="bbb" android:path="/ccc" android:mimeType="text/plain"/>
对应的设置数据参数方法
intent.setDataAndType(Uri.parse("aaa://bbb/ccc"), "text/plain");
不要采用这种方法:
intent.setData(Uri.parse("aaa://bbb/ccc"));
intent.setType("text/plain");
因为setType()会把setData()清空,从而产生意图不匹配的错误
采用隐式意图时:如果没有数据参数的情况下,只要意图对象中设置的动作和类别都出现在意图过滤器中,就能够跟过滤器匹配
注意:采用显示意图的话
<intent-filter>
<!-- 动作参数 -->
action android:name="cn.com.me"/>
<!-- 类别参数 -->
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
意图过滤器不用配置
用这个方法即可
intent.setClass(getApplicationContext(), OtherActivity.class);
在同一个应用中采用显示意图比较好,因为需要查找,所以用显示意图性能高一些
应用与应用之间的相互激活采用隐式意图比较好
- android基础--意图的学习
- Android基础 | 意图Intent讲解
- 学习Intent 的意图
- Android学习记录(7)—Intent中显示意图和隐式意图的用法
- Android学习记录--Intent中显示意图和隐式意图的用法
- Android学习 15 ->意图Intent
- Android Intent学习之 隐式意图&IntentFilter的作用
- River的Android学习笔记——Intent意图
- 隐式意图与显式意图的基础
- Android的隐式意图
- Android --Intent意图的讲解
- android 安装apk的意图
- android 显示意图的笔记
- Android的隐式意图
- android的隐式意图
- Android的Intent意图详解
- android基础学习5————隐式意图intent开启系统照相机
- Android 显示意图和隐式意图的区别
- map
- 系统分析师修养
- 32位程序移植到64位平台前的准备工作
- android基础--为应用添加新的Activity与参数传递
- 基于hive的dip-data-analyze
- android基础--意图的学习
- ubuntu 各版本更新源,速度快
- 原码、反码、补码之来龙去脉
- android基础--Activity的生命周期
- 用户在主界面点击返回键时,提示是否退出
- Android:数据存储方式
- asp net mvc3 ViewBag
- 微软推出Windows Intune 2.0云管理服务
- 什么是字节