隐式意图的剖析
来源:互联网 发布:魅族移动数据不能上网 编辑:程序博客网 时间:2024/06/11 12:40
一个页面跳转另一个页面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.sqf.intent2.MainActivity" > <Button android:onClick="click" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="隐式意图激活界面" /></RelativeLayout>
要跳转的界面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.sqf.intent2.SeconedActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第二个界面" /></RelativeLayout>
1>.在只指定android:name的时候
package com.sqf.intent2;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void click(View v){Intent intent = new Intent();intent.setAction("com.sqf.intent2.open2"); //与配置文件中的action一样}}
package com.sqf.intent2;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;public class SeconedActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_seconed);}}
清单文件:
<intent-filter > <!-- 过滤器 --> <action android:name="com.sqf.intent2.open2"/> <!-- 动作 action中的name是任意的,一般前面是包名,后面是期作用--> </intent-filter>
错误:
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.sqf.intent2.open2 }
就是Activity没有被找到,指定包下的文件
2>.当指定mimeType的时候
mimeType可以自己指定,也可以自己指定
public void click(View v){ //<span style="font-family: Arial, Helvetica, sans-serif;">MainActivity 类</span>Intent intent = new Intent();intent.setAction("com.sqf.intent2.open2"); //与配置文件中的action一样intent.setType("application/person");startActivity(intent);}
<activity android:name=".SeconedActivity" > <intent-filter > <!-- 过滤器 --> <action android:name="com.sqf.intent2.open2"/> <!-- 动作 action中的name是任意的,一般前面是包名,后面是期作用--> <data android:mimeType="application/person"/> <!-- 指定类型 自定义的类型--> </intent-filter> </activity>
错误:
04-18 04:56:18.185: E/AndroidRuntime(1364): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent
{ act=com.sqf.intent2.open2 typ=application/person }
乃是没找到,原因没有指定intent.addCategory(Intent.CATEGORY_DEFAULT); 有的加有的版本没有加
3>.加上Intent.CATEGORY_DEFAULT
<activity android:name=".SeconedActivity" > <intent-filter > <!-- 过滤器 --> <action android:name="com.sqf.intent2.open2"/> <!-- 动作 action中的name是任意的,一般前面是包名,后面是期作用--> <data android:mimeType="application/person"/> <!-- 指定类型 --><category android:name="android.intent.category.DEFAULT"/> <!-- <span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.3px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> 指定以何种方式去服务Intent请求的动作</span> --> </intent-filter><pre name="code" class="html">activity/><span style="font-family: Arial, Helvetica, sans-serif;"> </span>
public void click(View v){Intent intent = new Intent();intent.setAction("com.sqf.intent2.open2"); //与配置文件中的action一样intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setType("application/person");startActivity(intent);}
可以运行
4>.只在配置文件中加这句代码:<data android:scheme="jianren" /> 约束
<activity android:name=".SeconedActivity" > <intent-filter > <!-- 过滤器 --> <action android:name="com.sqf.intent2.open2"/> <!-- 动作 action中的name是任意的,一般前面是包名,后面是期作用--> <data android:mimeType="application/person"/> <!-- 指定类型 --><category android:name="android.intent.category.DEFAULT"/> <data android:scheme="jianren" /> </intent-filter><pre name="code" class="html"><activity/>
public void click(View v){Intent intent = new Intent();intent.setAction("com.sqf.intent2.open2"); //与配置文件中的action一样intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setType("application/person");startActivity(intent);}
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent
{ act=com.sqf.intent2.open2 cat=[android.intent.category.DEFAULT] typ=application/person }
错误原因:你在配置文件中<data android:scheme="jianren" /> 指定了约束但是没有类型,就是你打人,你要知道打谁吧
5>.同时在MainActivity的click方法中指定打人的类型 intent.setData(Uri.parse("jianren:张三"));
<activity android:name=".SeconedActivity" > <intent-filter > <!-- 过滤器 --> <action android:name="com.sqf.intent2.open2"/> <!-- 动作 action中的name是任意的,一般前面是包名,后面是期作用--> <data android:mimeType="application/person"/> <!-- 指定类型 --><category android:name="android.intent.category.DEFAULT"/> <data android:scheme="jianren" /> </intent-filter><pre name="code" class="html"><activity/>
public void click(View v){Intent intent = new Intent();intent.setAction("com.sqf.intent2.open2"); //与配置文件中的action一样intent.addCategory(Intent.CATEGORY_DEFAULT); //默认类别intent.setType("application/person");//URL:统一资源定位符 URI:统一资源标识符 URL是URI的子集intent.setData(Uri.parse("jianren:张三"));startActivity(intent);}
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent
{ act=com.sqf.intent2.open2 cat=[android.intent.category.DEFAULT] dat=jianren:张三 }
原因:
intent类的setData方法:
Set the data this intent is operating on. This method automatically clears any type that was previously set bysetType
or setTypeAndNormalize
.
所以,上面设置的setType方法失效
同理:如果把setType方法放在前面的话
setType方法的解释:
This is used to create intents that only specify a type and not data, for example to indicate the type of data to return.
This method automatically clears any data that was previously set (for example bysetData
).
解决这个问题的办法:调用intent的setDataAndType方法,同时设置Date和type,就可以解决这个问题
<activity android:name=".SeconedActivity" > <intent-filter > <!-- 过滤器 --> <action android:name="com.sqf.intent2.open2"/> <!-- 动作 action中的name是任意的,一般前面是包名,后面是期作用--> <data android:mimeType="application/person"/> <!-- 指定类型 --><category android:name="android.intent.category.DEFAULT"/> <data android:scheme="jianren" /> </intent-filter>
public void click(View v){//打 action//人 数据 Type//附加的数据 Category 类别Intent intent = new Intent();intent.setAction("com.sqf.intent2.open2"); //与配置文件中的action一样intent.addCategory(Intent.CATEGORY_DEFAULT); //默认类别//intent.setType("application/person");//intent.setData(Uri.parse("jianren:张三"));intent.setDataAndType(Uri.parse("jianren:张三"),"application/person");//关于约束后面是否加//,意思就是jianren:后面是否加双斜杠,是根据,是否有主机名//当有主机名的时候,jianren://baidu.com//一般的uri scheme://host:port/path 没有主机名不用加//startActivity(intent);}
可以运行accept
说一个点击按钮访问百度的例子:
- 隐式意图的剖析
- Android的隐式意图
- Intent 的 隐式意图
- Android的隐式意图
- 隐式意图的使用
- android的隐式意图
- 隐式意图与显式意图的基础
- 显式意图和隐式意图的区别小结
- Intent中显示意图和隐式意图的用法
- Android 显示意图和隐式意图的区别
- Android 显示意图和隐式意图的区别
- 显示意图和隐式意图的使用场景
- Intent中显示意图和隐式意图的用法
- 显式意图&隐式意图
- 显式意图和隐式意图
- Android 隐式意图和显示意图
- 显示意图和隐式意图
- Android 隐式意图和显示意图
- 数据持久化
- iOS定义常量除了用宏还可以用FOUNDATION_EXPORT
- 使用沃通免费证书在tomca下t配置https实战
- 安卓学习笔记之使用WindowManger创建浮窗
- android studio之genymotion问题
- 隐式意图的剖析
- eclipse+cdt 代码编译通过 正常运行 编辑器却报错
- 再论相机标定
- 4月17日比较想学的东西
- 从Android开发谈谈对编程语言的看法
- ios系统中通知栏扩展开发入门
- static_const配合void*实现的多态
- 批量扫描工具btscan
- 【笔试/面试】MFC(七)—— 对话框