Android入门:Intent入门(1)
一、Intent介绍
如果Android要做什么事件,则肯定与Intent有关;Intent能够做的动作有很多,比如打电话动作、发短信动作、跳转界面动作等;
二、Intent继承结构
java.lang.Object
|- android.content.Intent
三、Intent应用
1.打电话
此处实现一个如下界面的程序:
点击拨打按钮后:
步骤1:
在Android-manifest.xml中的<application>前面加入<uses-permission>元素:
<uses-permission android:name="android.permission.CALL_PHONE" />
步骤2:
在Activity中加入Intent语句:
package org.xiazdong; 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.EditText; public class IntentActivity extends Activity { private Button btn1; private EditText et1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); et1 = (EditText)this.findViewById(R.id.et1); btn1 = (Button)this.findViewById(R.id.btn1); btn1.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_CALL); //也可以使用intent.setAction("android.intent.action.CALL"); intent.setData(Uri.parse("tel:"+et1.getText())); startActivity(intent); } }); } }
3.发短信
程序界面如下:
发送短信界面
5556收到短信界面:
package org.xiazdong; 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.EditText; public class IntentActivity extends Activity { private Button btn1; private EditText et1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); et1 = (EditText)this.findViewById(R.id.et1); btn1 = (Button)this.findViewById(R.id.btn1); btn1.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_SENDTO); intent.setData(Uri.parse("sms:"+et1.getText())); startActivity(intent); } }); } }