Android学习之 Intent
2011-03-02 23:39
399 查看
Intent 是Android Activity之间传递数据的桥梁!
我们要从一个界面切换到另一个界面需要用到它,同时,应用间的相互调用也要用它!
Android uses Intents to do specific jobs within applications.
Android通过Intent在application之间完成指定的任务。
下面,是一段我从《Android, A programmer’s guide》中摘的一段话,
Once you master the use of Intents, a whole new world of application
development will be open to you. This section defines what an Intent is and how it is used.
An Intent is Android’s method for relaying certain information from one Activity to
another. An Intent, in simpler terms, expresses to Android your intent to do something.
You can think of an Intent as a message passed between Activities. For example, assume
that you have an Activity that needs to open a web browser and display a page on your
Android device. Your Activity would send an “intent to open x page in the web browser,”
known as a WEB_SEARCH_ACTION Intent, to the Android Intent Resolver. The Intent
Resolver parses through a list of Activities and chooses the one that would best match
your Intent; in this case, the Web Browser Activity. The Intent Resolver then passes your
page to the web browser and starts the Web Browser Activity.
Intents 分为两大类:
● Activity 间动作 Intents
这些Intents用于在你的Application中呼叫其他的Activities.
且只能一个Activity可以处理这个Intent, 例如,浏览一个网页,你需要打开一个Web Browser Activity.
● 广播型Intents
这些Intents 发送给多个 Activities 进行处理.
一个广播型Intent的例子是,当手机电量在低水平时,Android将会发出一个当前电量level的消息,任何
Activity都可以进行处理,并执行相关的动作,例如消息某些Activity当电量低于某个特定点时!
我学习的时候用了一个简单的例子:
在上面的代码中,我们可以看到,我在这个Activity中包含两个控件:一个是TextView MyTextView, 另一个是Button MyButton.
MyTextView用来显示一些标志信息,MyButton用于切换到另一个Activity中!
findViewByID(int resourceID)只能被用在View 上面。这些View 是放进布局中的。而这个布局被Activity 使用setContentView()
来加载的。
类似于As3一样,需要给Android添加一OnClick监听!不过java的做法与As3不太一样!
首先需要需要声明一个类实现onClickListener这个接口!在这类中我们需要覆盖onClick方法!我是初学者,对于怎么学我不知道,不过
看了视频后,我学会了!有两种方法:
方法1. 将鼠标放在MyButtonListener上,会出现一人提示:
点击Add unimplemented methods就可以了!
方法2.点击右键或者按Alt+Shift+S,选择Override/Implements methods
然后我们往这个OnClick函数中写代码:
Intent intent=new Intent() 创建了一个intent实例。
intent.setClass(Android01.this,Activity02.class)
下面是setClass的声明与参数:
public IntentsetClass(Context packageContext, Class<?> cls)
Parameters
packageContext: A Context of the application package implementing this class.
实现这个类的应用程序包的Context。我猜测这个Context应该理解为实例!还请高手指证!
cls: The class name to set, equivalent to
要设置的类名!相当于
意思是告诉Android, Context要启动一个Activity,他的名这是cls。
intent.putExtra("Key", "NeuglsWorkStudio");
这个很简单,往intent中加入一个Key-Value对!
Android01.this.startActivity(intent);
启动Activity.
这就是第一个Activity的任务!
下面是第二个Activity的程序:
他从启动他的Intent获得数据,然后在一个TextView中显示出来。
在这个Activity中还有一个Button CreateMsgBtn. 这个Button的OnClick代码中演示了Intent如何用于启动另一个App!上面代码是演示如何发送消息。
下面是实现监听onClick事件的另一种写法:
我们要从一个界面切换到另一个界面需要用到它,同时,应用间的相互调用也要用它!
Android uses Intents to do specific jobs within applications.
Android通过Intent在application之间完成指定的任务。
下面,是一段我从《Android, A programmer’s guide》中摘的一段话,
Once you master the use of Intents, a whole new world of application
development will be open to you. This section defines what an Intent is and how it is used.
An Intent is Android’s method for relaying certain information from one Activity to
another. An Intent, in simpler terms, expresses to Android your intent to do something.
You can think of an Intent as a message passed between Activities. For example, assume
that you have an Activity that needs to open a web browser and display a page on your
Android device. Your Activity would send an “intent to open x page in the web browser,”
known as a WEB_SEARCH_ACTION Intent, to the Android Intent Resolver. The Intent
Resolver parses through a list of Activities and chooses the one that would best match
your Intent; in this case, the Web Browser Activity. The Intent Resolver then passes your
page to the web browser and starts the Web Browser Activity.
Intents 分为两大类:
● Activity 间动作 Intents
这些Intents用于在你的Application中呼叫其他的Activities.
且只能一个Activity可以处理这个Intent, 例如,浏览一个网页,你需要打开一个Web Browser Activity.
● 广播型Intents
这些Intents 发送给多个 Activities 进行处理.
一个广播型Intent的例子是,当手机电量在低水平时,Android将会发出一个当前电量level的消息,任何
Activity都可以进行处理,并执行相关的动作,例如消息某些Activity当电量低于某个特定点时!
我学习的时候用了一个简单的例子:
package nw.neugls.first; 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; import android.widget.TextView; public class Android01 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView MyTextView=(TextView)findViewById(R.id.MyTextView); Button MyButton=(Button)findViewById(R.id.MyButton); MyTextView.setText("My first Activity"); MyButton.setText("Click me"); MyButton.setOnClickListener(new MyButtonListener()); } class MyButtonListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent=new Intent(); intent.setClass(Android01.this,Activity02.class); intent.putExtra("Key", "NeuglsWorkStudio"); Android01.this.startActivity(intent); } } }
在上面的代码中,我们可以看到,我在这个Activity中包含两个控件:一个是TextView MyTextView, 另一个是Button MyButton.
MyTextView用来显示一些标志信息,MyButton用于切换到另一个Activity中!
findViewByID(int resourceID)只能被用在View 上面。这些View 是放进布局中的。而这个布局被Activity 使用setContentView()
来加载的。
类似于As3一样,需要给Android添加一OnClick监听!不过java的做法与As3不太一样!
首先需要需要声明一个类实现onClickListener这个接口!在这类中我们需要覆盖onClick方法!我是初学者,对于怎么学我不知道,不过
看了视频后,我学会了!有两种方法:
方法1. 将鼠标放在MyButtonListener上,会出现一人提示:
点击Add unimplemented methods就可以了!
方法2.点击右键或者按Alt+Shift+S,选择Override/Implements methods
然后我们往这个OnClick函数中写代码:
public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent=new Intent(); intent.setClass(Android01.this,Activity02.class); intent.putExtra("Key", "NeuglsWorkStudio"); Android01.this.startActivity(intent); }
Intent intent=new Intent() 创建了一个intent实例。
intent.setClass(Android01.this,Activity02.class)
下面是setClass的声明与参数:
public IntentsetClass(Context packageContext, Class<?> cls)
Parameters
packageContext: A Context of the application package implementing this class.
实现这个类的应用程序包的Context。我猜测这个Context应该理解为实例!还请高手指证!
cls: The class name to set, equivalent to
setClassName(context, cls.getName()).
要设置的类名!相当于
setClassName(context, cls.getName()).
意思是告诉Android, Context要启动一个Activity,他的名这是cls。
intent.putExtra("Key", "NeuglsWorkStudio");
这个很简单,往intent中加入一个Key-Value对!
Android01.this.startActivity(intent);
启动Activity.
这就是第一个Activity的任务!
下面是第二个Activity的程序:
他从启动他的Intent获得数据,然后在一个TextView中显示出来。
package nw.neugls.first; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class Activity02 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity02); TextView tvActivity2=(TextView)findViewById(R.id.tvActivity02); Intent intent=getIntent(); String str=intent.getStringExtra("Key"); Button CreateMsgBtn=(Button)findViewById(R.id.btnCreateMsg); CreateMsgBtn.setText("Create Message"); CreateMsgBtn.setOnClickListener(new CreateMsgBtnListener()); tvActivity2.setText(str); } class CreateMsgBtnListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub Uri uri=Uri.parse("smsto://13539441914"); Intent intent=new Intent(Intent.ACTION_SENDTO,uri); intent.putExtra("sms_body", "Write to neugls"); startActivity(intent); } } }
在这个Activity中还有一个Button CreateMsgBtn. 这个Button的OnClick代码中演示了Intent如何用于启动另一个App!上面代码是演示如何发送消息。
下面是实现监听onClick事件的另一种写法:
Button CreateMsgBtn=(Button)findViewById(R.id.btnCreateMsg); CreateMsgBtn.setText("Create Message"); CreateMsgBtn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ Intent intent=new Intent(Android01.this,Activity02.class); //其他代码 } }); ;
相关文章推荐
- Android 学习笔记12:Intent和Intent-filter,
- Android Intent学习之 ApiDemos
- Android项目学习—Intent的作用详解
- Android Activity和Intent机制学习笔记
- android学习五 Intent
- android之intent学习
- android学习 关于用intent 传递数据
- Android学习第二天:Intent传输数据、常用控件和Activity生命周期
- Android之Intent学习
- Android学习篇章50-MediaStore-Intent启动照相
- android学习之intent跳转3引用值得传递
- Android文档学习03_Intent
- 《android 学习》三、Intent 的概念及应用
- Android Activity和Intent机制学习笔记
- Android Activity和Intent机制学习笔记
- Android学习总结(三)——IntentService的用法
- Android学习之——intent-fliter配置之data属性
- Android官方文档学习01多屏幕适配&Fragment间通信&Intent分享
- Android学习之Intent使用
- Android开发学习笔记:浅谈显示Intent和隐式Intent