[转载]Android学习之 Intent – 潺缘 – 博客园.
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当电量低于某个特定点时!
我学习的时候用了一个简单的例子:
01 |
package nw.neugls.first; |
03 |
import android.app.Activity; |
04 |
import android.content.Intent; |
05 |
import android.os.Bundle; |
06 |
import android.view.View; |
07 |
import android.view.View.OnClickListener; |
08 |
import android.widget.Button; |
09 |
import android.widget.TextView; |
11 |
public class Android01 extends Activity { |
12 |
/** Called when the activity is first created. */ |
14 |
public void onCreate(Bundle savedInstanceState) { |
15 |
super .onCreate(savedInstanceState); |
16 |
setContentView(R.layout.main); |
18 |
TextView MyTextView=(TextView)findViewById(R.id.MyTextView); |
19 |
Button MyButton=(Button)findViewById(R.id.MyButton); |
21 |
MyTextView.setText( "My first Activity" ); |
22 |
MyButton.setText( "Click me" ); |
23 |
MyButton.setOnClickListener( new MyButtonListener()); |
27 |
class MyButtonListener implements OnClickListener{ |
30 |
public void onClick(View arg0) { |
32 |
Intent intent= new Intent(); |
33 |
intent.setClass(Android01. this ,Activity02. class ); |
34 |
intent.putExtra( "Key" , "NeuglsWorkStudio" ); |
35 |
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函数中写代码:
1 |
public void onClick(View arg0) { |
3 |
Intent intent= new Intent(); |
4 |
intent.setClass(Android01. this ,Activity02. class ); |
5 |
intent.putExtra( "Key" , "NeuglsWorkStudio" ); |
6 |
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中显示出来。
01 |
package nw.neugls.first; |
03 |
import android.app.Activity; |
04 |
import android.content.Intent; |
05 |
import android.net.Uri; |
06 |
import android.os.Bundle; |
07 |
import android.view.View; |
08 |
import android.view.View.OnClickListener; |
09 |
import android.widget.Button; |
10 |
import android.widget.TextView; |
12 |
public class Activity02 extends Activity { |
15 |
protected void onCreate(Bundle savedInstanceState) { |
17 |
super .onCreate(savedInstanceState); |
19 |
setContentView(R.layout.activity02); |
20 |
TextView tvActivity2=(TextView)findViewById(R.id.tvActivity02); |
22 |
Intent intent=getIntent(); |
24 |
String str=intent.getStringExtra( "Key" ); |
26 |
Button CreateMsgBtn=(Button)findViewById(R.id.btnCreateMsg); |
27 |
CreateMsgBtn.setText( "Create Message" ); |
28 |
CreateMsgBtn.setOnClickListener( new CreateMsgBtnListener()); |
30 |
tvActivity2.setText(str); |
33 |
class CreateMsgBtnListener implements OnClickListener{ |
36 |
public void onClick(View arg0) { |
40 |
Intent intent= new Intent(Intent.ACTION_SENDTO,uri); |
41 |
intent.putExtra( "sms_body" , "Write to neugls" ); |
42 |
startActivity(intent); |
在这个Activity中还有一个Button CreateMsgBtn. 这个Button的OnClick代码中演示了Intent如何用于启动另一个App!上面代码是演示如何发送消息。
下面是实现监听onClick事件的另一种写法:
01 |
Button CreateMsgBtn=(Button)findViewById(R.id.btnCreateMsg); |
02 |
CreateMsgBtn.setText( "Create Message" ); |
03 |
CreateMsgBtn.setOnClickListener( new OnClickListener(){ |
05 |
public void onClick(View v){ |
06 |
Intent intent= new Intent(Android01. this ,Activity02. class ); |