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

  • 一:布局文件先设计拨号器的简单界面,可以先用画图软件构思 界面
  • 二 :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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值