- 浏览: 233838 次
- 性别:
- 来自: 广东
文章分类
最新评论
-
ryutaku:
楼主,我专门找回原业的用户名和密码来回复你的。view.get ...
Android中ListView.getCount()与ListView.getChildCount()区别和OnScrollListener()各个参数的区别 -
ponce:
不错哦,正好用上
android HTTP 通信, XML 解析, 通过 Hander 实现异步消息处理(2) -
sunny78117:
gundumw100 写道能不能在Xml配置中设置啊,这样硬编 ...
阻止EditText弹出输入法 -
sulanyan29:
老大,求一份原码。谢谢了: dk_application@16 ...
Android防火墙+流量统计代码 -
seventhfox:
很好的办法 谢谢分享...
阻止EditText弹出输入法
以下内容Sinfrancis版权所有,专注请注明来自 http://mdev.cc/dev
在Android中,传递数据使用Intent,Intent相当于各个Activity之间的桥梁,可以传递数据,可以通过Intent启动另外一个Activity。
Intent有显式和隐式之分,显式的是直接什么要启动的组件,比如Service或者Activity,隐式的通过配置的datatype、 url、action来找到匹配的组件启动。
此程序目的:
1、显式启动Activity和service
2、通过隐式的变量,启动Activity和Service
先来看先我们定义的变量类:
程序主界面的代码:还有四个按钮,分别用于启动不同的组件:
以下分别是被启动的组件代码:
显式Activity和Service:
隐式启动的Activity和Service:
AndroidManifest.xml文件配置:
这个很重要,需要配置好才行,不然会出现AcitvityNotFoundException
在Android中,传递数据使用Intent,Intent相当于各个Activity之间的桥梁,可以传递数据,可以通过Intent启动另外一个Activity。
Intent有显式和隐式之分,显式的是直接什么要启动的组件,比如Service或者Activity,隐式的通过配置的datatype、 url、action来找到匹配的组件启动。
此程序目的:
1、显式启动Activity和service
2、通过隐式的变量,启动Activity和Service
先来看先我们定义的变量类:
package cc.androidos.intent; public class Book { //Intent的数据类型 public static String CONTENT_TYPE = "cc.android/intent.demo"; //Intent中的URL,这里要使用Content开头,不然会找不到组件 public static String CONTENT_URI = "content://test/"; }
程序主界面的代码:还有四个按钮,分别用于启动不同的组件:
package cc.androidos.intent; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; public class IntentDemo extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button firstbtn = (Button) findViewById(R.id.firstbtn); Button firstbtnservice = (Button) findViewById(R.id.firstbtnservice); Button secondbtn = (Button) findViewById(R.id.secondbtn); Button secondbtnservice = (Button) findViewById(R.id.secondbtnservice); firstbtn.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { //显式启动FirstIntentDemo Activity Intent i = new Intent(getApplicationContext(),FirstIntentDemo.class); startActivity(i); } }); firstbtnservice.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { //显式启动FirstService 后台服务 Intent i = new Intent(getApplicationContext(),FirstService.class); startService(i); } }); secondbtn.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { //通过Action uri和dataype启动Activity //程序会自动匹配到Intent-Filter配置中有(action属性)Action为Intent.ACTION_VIEW,并且数据类型(data)为cc.android/intent.demo的组件上 Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse(Book.CONTENT_URI), Book.CONTENT_TYPE); startActivity(intent); } }); secondbtnservice.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { //通过Action uri和dataype启动Service Intent intent = new Intent(); intent.setAction(Intent.ACTION_EDIT); intent.setDataAndType(Uri.parse(Book.CONTENT_URI), Book.CONTENT_TYPE); startService(intent); } }); } }
以下分别是被启动的组件代码:
显式Activity和Service:
package cc.androidos.intent; import android.app.Activity; import android.os.Bundle; public class FirstIntentDemo extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.firstintent); } } =============================================== package cc.androidos.intent; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class FirstService extends Service{ @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); String tag = "First Service on Create"; Log.d(tag,"This is the first service..."); } }
隐式启动的Activity和Service:
package cc.androidos.intent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; public class SecondIntentDemo extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String tag = "SecondIntentDemo onCreate.."; setContentView(R.layout.secondintent); Intent i = getIntent(); Log.d(tag, "intent type : " + i.getType()); Log.d(tag, "intent url : " + i.getData().toString()); } }
package cc.androidos.intent; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class SecondService extends Service { @Override public IBinder onBind(Intent arg0) { return null; } @Override public void onCreate() { super.onCreate(); String tag = "Second service "; Log.d(tag, "Startup second service... "); } }
AndroidManifest.xml文件配置:
这个很重要,需要配置好才行,不然会出现AcitvityNotFoundException
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cc.androidos.intent" android:versionCode="1" android:versionName="1.0.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".IntentDemo" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- First Intent Demo --> <activity android:name=".FirstIntentDemo" android:label="@string/app_name"> </activity> <!-- Second Intent Demo --> <activity android:name=".SecondIntentDemo" android:label="@string/app_name"> <intent-filter > <!--The intent filter parameters must match the intent datatype(mimeType) \ action --> <action android:name="android.intent.action.VIEW"/> <data android:mimeType="cc.android/intent.demo"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity> <service android:name=".FirstService" > </service> <service android:name=".SecondService" > <intent-filter > <!--The intent filter parameters must match the intent datatype(mimeType) \ action --> <action android:name="android.intent.action.EDIT"/> <data android:mimeType="cc.android/intent.demo"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </service> </application> </manifest>
发表评论
-
Fragments (Android官方文档中文版)
2011-09-14 16:32 2815概述 Fragment ... -
MediaScanner 研究
2011-05-03 19:41 1695转载http://www.iteye.com/topic/69 ... -
【Android】Uri、UriMatcher、ContentUris详解
2011-04-29 15:14 34861.Uri 通用资源标志符(Universal Resour ... -
Android事件处理
2011-01-04 11:07 2382转载:http://blog.csdn.net/G_rrrr/ ... -
Paint类常用方法
2010-11-20 15:04 1424Paint类常用方法: void setARGB(int ... -
自定义View 常用方法
2010-11-20 15:03 3855自定义View的常用方法: ... -
Android onSaveInstanceState和onRestoreInstanceState触发的时机
2010-11-16 21:46 4850先看Application Fundamentals上的一段 ... -
Activity有四种状态
2010-11-02 22:28 1690Activity有四种状态: 处于屏幕最前端的Activ ... -
介绍Android中与JSON相关的应用
2010-10-23 12:57 1884转载:http://www.j2bb.com/tr ... -
Android TextView(EditView)文字底部或者中间 加横线
2010-10-14 20:16 5126promotionLinkText = (TextView) ... -
转载:Handler方法小结(在线程中更新UI和了解HandlerThread类的用法)
2010-09-11 09:45 2719小结: * 1、向哪个Handler 发送消息,就必 ... -
卸载程序和监听卸载事件
2010-09-07 08:28 2421package com.TestUI; import j ... -
shape的使用
2010-08-20 17:47 2611<!—显示一条虚线 --> &l ... -
Android中ListView.getCount()与ListView.getChildCount()区别和OnScrollListener()各个参数的区别
2010-08-15 14:51 14440ListView.getCount()(实际上是 Adapte ... -
Android中Bitmap, Drawable, Byte之间的转化
2010-08-15 14:47 28161. Bitmap 转化为 byte ByteArrayO ... -
Uri常用方法
2010-07-29 15:47 11668Uri uri = Uri.parse("conte ... -
调用系统内部 searchUI的基本用法
2010-07-19 22:36 2960/* 按下键盘即调用搜索框 */ setDef ... -
Android改变窗口标题栏的布局
2010-07-15 14:35 2313一般应用的Title都是建立应用时在AndroidManife ... -
Activity之间实现动画的切换
2010-07-14 11:02 2261在startActivity 后调用 overridePend ... -
向Web站点发送GET请求、POST请求
2010-06-09 22:04 2760public class TestGetPost { /* ...
相关推荐
Andriod Intent使用代码举例
Android 通过Intent使用Bundle传递对象 Android开发中有时需要在应用中或进程间传递对象,下面详细介绍Intent使用Bundle传递对象的方法。 被传递的对象需要先实现序列化,而序列化对象有两种方式:java.io....
android Intent使用案例 含:播放多媒体、打电话、发短信、发送email、发邮件、google服务、组件component、action值自定义、显示地图/路径规划、选择应用、打开应用列表、搜索应用等意图实例。
Android-Intent使用方法详解 配合(http://blog.csdn.net/daiyibo123/article/details/51227160)博客查看。使用Android stdio编写。
Android中Intent使用、数据回写(显)
androidIntent使用技巧.pdf
android Intent使用技巧.pdf
最简单intent使用Activity切换实例 按button进入act2
intent使用源码实例.
Intent的示例,详细的注释,详细讲解了startActivityForResult的用法,以及Bundle跟Intent保存对象的机制.
源程序关于android平台中intent的使用小示例,仅供参考
初学移动应用公开发中的Android开发,实验四的主要内容为intent的使用,通过这一次实验,掌握基本的intent使用方法。 具体实验分析 实验第一步:阅读官方文档:intent 实验解析:本次实验共分为两个部分。第一个部分...
开发一年多android 对intent 一般用到的地方总结了一下!
这里总结一些重用的Intent使用,仅供参考。 下面直接给我学习的实例片段。 1,调用web浏览器 Uri myBlogUri = Uri.parse("http://kuikui.javaeye.com"); returnIt = new Intent(Intent.ACTION_VIEW, myBlogUri); 2...
Android开发中有时需要在应用中或进程间传递对象,下面详细介绍Intent使用Bundle传递对象的方法。 被传递的对象需要先实现序列化,而序列化对象有两种方式:java.io.Serializable和android.os.Parcelable Java中...
在Android中通过Intent使用Bundle传递对象源码
使用Intent打开网页
使用Intent拨打电话