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就封装 ...
随机推荐
- iOS结合导航控制器和标签栏控制器
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name=& ...
- android imageButton 透明图片
在Android有许多不规则button.例如: 这个时候,我们假设想做成不规则button的话.第一步就是搞一张边缘透明的png图片,然后用src指定到他.这个时候我们会发现,还没有达到要的效果.还 ...
- Myeclipse它显示了一个目录的结构,而不是包
今天Myeclipse新project,编写代码,查找workspace空间展示project在包装和class所有平行结构,看的很不顺,有两个原因,第一,您可能无法切换到Package worksp ...
- C#多线程编程实例 螺纹与窗口交互
C#多线程编程实例 螺纹与窗口交互 代码: public partial class Form1 : Form { //声明线程数组 Thread[] workThreads = new Thread ...
- 搭建 Linux 下 GitLab 服务器(转)
这两天因为项目需求需要搭建一个GitLab服务器,遇到了很多问题,参考了很多网络资料,终于搭建成功,在此把这个过程记录一下,利人利己. 一.最终目的 1,在Linux下创建GitLab服务器,客户端能 ...
- 【Android进阶】判断网络连接状态并自动界面跳转
用于判断软件打开时的网络连接状态,若无网络连接,提醒用户跳转到设置界面 /** * 设置在onStart()方法里面,可以在界面每次获得焦点的时候都进行检测 */ @Override protecte ...
- 允许debian wheezy支持IOS7+的iphone.
IOS更新, 连接到数据线,不能使用 我想复制iphone照片只能用于内itunes对? 于linux这里面其实很容易处理. 在这里,我们使用了一个相对较新的组件libimobiledevice 为 ...
- C/C++数据对齐汇总
C/C++数据对齐汇总 这里用两句话总结数据对齐的原则: (1)对于n字节的元素(n=2,4,8,...),它的首地址能被n整除,才干获得最好的性能: (2)如果len为结构体中长度最长的变量,s ...
- Maven+Spring
Maven+Spring 关于Maven Maven是一个用于项目构建的工具,通过它便捷的管理项目的生命周期.即项目的jar包依赖,开发,测试,发布打包. 做过.NET的人应该会联想到Nuget,是的 ...
- IE不能上网、有道云笔记不能联网、各种软件主页不能联网解决办法一
其他的办法我几乎都试过了,读者可以无搜一下,我的问题是,我用Lantern.exe,所以只要打开这个就可以了! 我一直不知道是这个问题,困扰了好久QAQ