Android两个注意事项.深入了解Intent和IntentFilter(两)
Intent对象是一组信息,我们能够通过设置其Action、Data、Category属性来指定启动哪个组件并完毕什么样的动作(包括动作所需的数据)。
所谓显示intent,指的是Intent已经明白了它将要启动哪个组件-通过指定Intent对象的Component属性实现。而隐式intent,指的是Intent不能确定它将要启动哪个组件(没有指定Component属性)-通过AndroidManifest.xml文件里的Intent
Filter来对组件进行筛选来确定启动的组件。
ComponentName comp=new ComponentName(ComponentAttr.this,SecondaryActivity.class);
Intent intent=new Intent();
intent.setComponent(comp);
startActivity(intent);
或者 startActivityForResult(intent,requestCode); //关闭启动的Activity会返回结果
<!-- 被intent启动的activity -->
<activity
android:name=".SecondaryActivity"
android:label="第二个Activity界面" >
<intent-filter>
<action android:name="action.CRAZYIT_ACTION"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
博主笔记1:除了上述Intent的setComponent方法,我们还能够利用setClass来指定须要启动的详细组件
显式intent能够通过设置其Component属性实现。而隐式intent就通过Intent
Filter来实现。
详细的说就是,我们事先设置好"意图"启动组件的相关信息(intent属性),然后再在其它组件的AndroidManifest.xml文件设置好对应的intent属性。当组件发出"意图"时。Android系统通过查找project文件AndroidManifest.xml(或者系统级组件)其它组件的<intent-filter/>相关信息来进行匹配。筛选得到满足"意图"条件的组件。
方式
public final String CUSTOME_ACTION="intent.action.CUSTOME_JIANG";//字符串能够随意
Intent intent=new Intent(); //创建一个Intent对象
intent.setAction(ActionAttr.CUSTOME_ACTION); //注意:ActionAttr为我们创建的类
startActivity(intent); //启动一个Activity
Intent intent=new Intent();
intent.setAction(Intent.ACTION_CALL); //当中ACTION_CALL为Intent类的静态成员变量,能够类直接调用
startActivity(intent);
2.使用Action、Category属性开发基本思路
public final String CUSTOME_ACTION="intent.action.CUSTOME_JIANG";//字符串能够随意
public final String CUSTOME_CATEGORY="intent.action.CUSTOME_CATEGORY";//字符串能够随意
Intent intent=new Intent(); //创建一个Intent对象
intent.setAction(ActionAttr.CUSTOME_ACTION); //注意:ActionAttr为我们创建的类
intent.addCategory(ActionAttr.CUSTOME_CATEGORY);
startActivity(intent); //启动一个Activity
Intent intent=new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);//返回Home桌面
startActivity(intent); //启动一个Activity
注意:这里无需设置AndroidManifest.xml
<activity
android:name=".SecondaryActivity"
android:label="第二个Activity界面" >
<intent-filter>
<action android:name="intent.action.JIANG_ACTION" />
<category android:name="intent.action.JIANG_CATEGORY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".ThirdActivity"
android:label="第三个Activity界面" >
<intent-filter>
<action android:name="intent.action.JIANG_ACTION" />
<category android:name="intent.action.JIANG_CATEGORY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
博主笔记2:实际上。我们在开发包括"意图"的应用程序中,Action属性和Category属性是配合使用的。由于,Android系统会给主动Activity在AndroidManifest.xml中默认一个Action属性和Category属性。即:
//应用程序入口
3.源码
package com.example.android_intent_2;
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 ActionCateAttr extends Activity {
//自己定义一个action常量org.crazyit.
public final static String CRAZYIT_ACTION="intent.action.JIANG_ACTION";
public final static String CRAZYIT_CATEGORY="intent.action.JIANG_CATEGORY";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Button btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//创建一个Intent对象
Intent intent=new Intent();
intent.setAction(ActionCateAttr.CRAZYIT_ACTION); //设置action属性
intent.addCategory(ActionCateAttr.CRAZYIT_CATEGORY); //设置category属性
startActivity(intent);
}});//为btn注冊一个事件监听器对象 /*返回桌面按钮*/
Button btn1=(Button)findViewById(R.id.home);
btn1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//创建一个Intent对象
Intent intent=new Intent();
intent.setAction(Intent.ACTION_MAIN); //设置action属性
intent.addCategory(Intent.CATEGORY_HOME);//设置category属性
startActivity(intent);
}});//为btn注冊一个事件监听器对象
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android_intent_2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ActionCateAttr"
android:label="第一个Activity界面" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <activity
android:name=".SecondaryActivity"
android:label="第二个Activity界面" >
<intent-filter>
<action android:name="intent.action.JIANG_ACTION" />
<category android:name="intent.action.JIANG_CATEGORY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity> <activity
android:name=".ThirdActivity"
android:label="第三个Activity界面" >
<intent-filter>
<action android:name="intent.action.JIANG_ACTION" />
<category android:name="intent.action.JIANG_CATEGORY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjYzNzUwMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
这里须要注意的是,Type属性和Data属性通常会出现相互覆盖的情况,假设希望Intent既有Data属性也有Type属性,必须通过setDataAndType()方法来实现。
这里须要注意的是,Data属性仅仅接受一个Uri对象。一个Uri对象通常通过例如以下形式的字符串来表示:
Intent intent=new Intent(); //创建一个Intent对象
String data="content://com.android.contacts/contacts/1";
Uri uri=Uri.parse(data); //将字符串转换为Uri
intent.setAction(Intent.ACTION_VIEW); //设置Intent对象Action属性
intent.setData(uri); //设置Intent对象Data属性
startActivity(intent);
或者
Intent intent=new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("content://com.android.contacts/contacts/1"));
startActivity(intent);
在Android中读取电话信息时,要注意增加
<use-permission android:name="android.permission.READ_CONTACTS"/>
在android中使用BroadcastReceiver时
<use-permission android:name="android.permission.RECEIVE_SMS"/>
在android中使用有关的文件下载功能时,要使用到的
<use-permission android:name="android.permission.INTERNET"/>
<use-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
package com.android.android_intent_4;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*第一个按键功能:打开网页*/
Button btn1=(Button)findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener(){ //为按钮1注冊一个事件监听器对象 @Override
public void onClick(View v) {
//1.创建Intent
Intent intent=new Intent();
//2.设置action、data属性
String data="http://www.baidu.com";
Uri uri=Uri.parse(data); //将字符串转化为Uri-通用资源标识
intent.setAction(Intent.ACTION_VIEW); //设置intent属性为系统提前定义的Intent.ACTION_VIEW
intent.setData(uri); //为intent设置数据属性。用于传递数据
//3.启动Activity
startActivity(intent);
}}); /*第二个按键功能:编辑标识为1的联系人*/
Button btn2=(Button)findViewById(R.id.button2);
btn2.setOnClickListener(new OnClickListener(){ //为按钮1注冊一个事件监听器对象 @Override
public void onClick(View v) {
//1.创建Intent
Intent intent=new Intent();
//2.设置action、data属性
intent.setAction(Intent.ACTION_EDIT); //设置intent属性为系统提前定义的Intent.ACTION_VIEW
intent.setData(Uri.parse("content://com.android.contacts/contacts/1")); //为intent设置数据属性。依据指定的字符解析出Uri对象
//3.启动Activity
startActivity(intent);
}}); /*第三个按键功能:拨打电话18819465188*/
Button btn3=(Button)findViewById(R.id.button3);
btn3.setOnClickListener(new OnClickListener(){ //为按钮1注冊一个事件监听器对象
@Override
public void onClick(View v) {
//1.创建Intent
Intent intent=new Intent();
//2.设置action、data属性
intent.setAction(Intent.ACTION_DIAL); //设置intent属性为系统提前定义的Intent.ACTION_VIEW
intent.setData(Uri.parse("tel:18819465188")); //依据指定的字符解析出Uri对象
//3.启动Activity
startActivity(intent);
}});
} }

Intent intent=new Intent(); //创建一个Intent对象
String data="lee://www.fkjava.org:8888/mypath";
Uri uri=Uri.parse(data); //将字符串转换为Uri
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri); //设置Intent对象Data属性
startActivity(intent);
或者
Intent intent=new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("lee://www.fkjava.org:8888/mypath"));
startActivity(intent);
<data android:mimeType=""
android:host=""
android:port=""
android:path=""
android:pathPrefix=""
android:pathPattern=""/>
版权声明:本文博主原创文章。博客,未经同意不得转载。
Android两个注意事项.深入了解Intent和IntentFilter(两)的更多相关文章
- android学习日记20--连接组件之Intent和IntentFilter
上次刚了解完Android的四大组件,现在学习组件间通信的Intent和IntentFilter 一.Intent 1.简述 Intent(意图)在应用程序运行时连接两个不同组件,是一种运行时的绑定机 ...
- Android应用程序组件之间的通信Intent和IntentFilter
Android应用程序的基本组件,这些基本组建除了Content Provider之外,几乎全部都是依靠Intent对象来激活和通信的. 下面介绍Intent类,并通过例子来说明Intent一般用法 ...
- Android编程: fragment组件、菜单和Intent组件
学习内容:fragment组件.菜单和Intent组件 ====fragment组件====1.fragment是一种自我容纳,模块化的,嵌入在一个Activity里面的视图组件 可以在运行时动 ...
- intent,实现两个活动之间数据的传递
一.Intent 可以启动一个活动,也可以在启动活动的时候传递数据.intent中提供了putExtra()方法,它可以把我们想要传递的数据暂存在intent中,启动了另一个活动后,通过getInte ...
- Android Library开发注意事项
Android Library开发注意事项 App Module添加依赖Android Library时可以设置library的优先级, 在编译时,app按照library从低到高的优先级依次与每个l ...
- Android开发学习之浅谈显示Intent和隐式Intent
Intent寻找目标组件的两种方式: 显式Intent:通过指定Intent组件名称来实现的,它一般用在知道目标组件名称的前提下,一般是在相同的应用程序内部实现的. 隐式Intent:通过Intent ...
- Android的Intent和IntentFilter应用说明一例
很多人对文档中的Intent和IntentFilter不理解是什么意思,我这里举例解释下. Intent字面意思就是目标,目的.通俗一点,需要达成某些目标,则需要提供一些动作,这些目标的分类,以及达成 ...
- Android 之Activity启动模式(二)之 Intent的Flag属性
首页博客链接关于我留言板 前面介绍了通过launchMode设置Activity的启动模式.本章接着介绍Activity的启动模式相关内容,讲解的内容是Intent与启动模式相关的Flag,以及and ...
- android Intent和IntentFilter
android的应用程序包含三种重要的组件:Activity.Service.BroadcastReceiver,应用程序采用一致的方式来启动他们——都是依靠Intent来进行启动.Intent就封装 ...
随机推荐
- ZOJ Problem Set - 2563 Long Dominoes 【如压力dp】
称号:ZOJ Problem Set - 2563 Long Dominoes 题意:给出1*3的小矩形.求覆盖m*n的矩阵的最多的不同的方法数? 分析:有一道题目是1 * 2的.比較火.链接:这里 ...
- 开源Math.NET基础数学类库使用(04)C#解析Matrix Marke数据格式
原文:[原创]开源Math.NET基础数学类库使用(04)C#解析Matrix Marke数据格式 开源Math.NET基础数学类库使用系列文章总目录: 1.开源.NET基础数学计算组件Math. ...
- Android五个布局
Android五大布局Layout 1,LinearLayout 线性布局(能够嵌套使用): 制定线性布局的排列方式:水平排列 horizontal.垂直排列 vertical eg: android ...
- AsyncSocket长连接棒包装问题解决
project正在使用长连接快来server沟通.因此,指定我们的协议前两个字节为数据长度来区分数据包 app这边数据有两种传输形式: 1.app主动请求所须要的数据: 2.app异步接收来自服务端的 ...
- Net中的反应式编程
Net中的反应式编程(Reactive Programming) 系列主题:基于消息的软件架构模型演变 一.反应式编程(Reactive Programming) 1.什么是反应式编程:反应式编程 ...
- 我学cocos2d-x (两) 采用Delegate(信托)
Delegate(信托)什么 Delegate是ios开发中的一个概念,主要是为了让类A中的功能,放到类B中来实现,这样能够合理的把功能划分到不同的文件里进行实现,从而更好的实现模块的分离.如UIAp ...
- RH133读书笔记(8)-Lab 8 Manage Network Settings
Lab 8 Manage Network Settings Goal: To build skills needed to manually configure networking Estimate ...
- 《web全栈工程师的自我修养》阅读笔记
在买之前以为这本书是教你怎么去做一个web全栈工程师,以及介绍需要掌握的哪些技术的书,然而看的过程中才发现,是一本方法论的书.读起来的感觉有点像红衣教主的<我的互联网方法论>,以一些自己的 ...
- 无法使用SQL login去登陆SQL Server - 'Password did not match'
原文:无法使用SQL login去登陆SQL Server - 'Password did not match' 出自:http://blogs.msdn.com/b/apgcdsd/archive/ ...
- 物理卷操作命令:pvcreate,pvscan,pvdisplay.卷组操作命令:vgcreate,vgdisplay. (转)
新硬盘创建LVM系统过程. 物理卷操作命令:pvcreate,pvscan,pvdisplay. 卷组操作命令:vgcreate,vgdisplay. 逻辑卷操作命令:lvcreate,lvdispl ...