Android实现打电话的功能-使用Intent和AndroidManifset.xml中加入权限

来源:互联网 发布:网络舆情监测员 编辑:程序博客网 时间:2024/06/11 03:33
  • 一:布局文件先设计拨号器的简单界面,可以先用画图软件构思 界面
  • 二 :Activity中进行获取EditText中的电话号码,然后点击,使用Intent(意图)进行实现打电话的功能
    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(“tel:”+ mobile));
  • 三:注意必须在AndroidManifset,xml文件进行打电话的权限设置

    <uses-permission android:name="android.permission.CALL_PHONE" />

核心源代码


public class PhoneDuanXINActivity extends Activity {  private EditText mobileText;             //在这里写个成员变量,就可以直接调用  @Override  public void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.main);      mobileText = (EditText)this.findViewById(R.id.mobile);    //调用成员变量mobileText   强转给右边      // 使用匿名类进行加监听        Button button = (Button)this.findViewById(R.id.button);      button.setOnClickListener(new View.OnClickListener(){                 public void onClick(View v){              String mobile = mobileText.getText().toString();  //得到了用户输入的手机号              Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ mobile ));  //意图    Intent(行为, 行为数据)   intent类用于描述一个应用将会做什么事          在windos中就有种东西  开始菜单》》运行》》 http://www.baidu.com    它可以直接打开,这是为什么?   是因为浏览器   认出了http:               startActivity(intent); //这就是内部类访问外部类的实例          }      });  }  }  

strings.xml

<?xml version="1.0" encoding="utf-8"?>  <resources>  <string name="hello">下午好</string>  <string name="app_name">摩托罗拉手机 拨号器</string>  <string name="mobile">请输入号码</string>  <string name="button">拨打</string>  </resources>  

main.xml

<?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:orientation="vertical" >  <TextView              android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="@string/mobile"       />  <EditText             android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:id = "@+id/mobile"         />  <Button      android:layout_width ="wrap_content"      android:layout_height="wrap_content"      android:text = "@string/button"      android:id = "@+id/button"      />  <p style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px; text-align: left; "><span style="font-family: 'Microsoft YaHei'; color: rgb(102, 102, 102); "><span style="font-size:13px;">如图所示这三个控件是垂直摆放的,所以要使用线性布局来搁置显示控件</span></span></p><p style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px; text-align: left; "><strong><span style="font-family: 'Microsoft YaHei'; color: rgb(102, 102, 102); "><span style="font-size:13px;">效果图:</span></span></strong></p>  </LinearLayout>  

这里其实最重要的就要属权限设置,打电话权限, 还有理解 intent 意图 (行为,行为数据)的用法

0 0
原创粉丝点击