Android打电话功能权限报错,Android基础——拨打电话

本例将通过Intent(意图)调用系统拨号器实现拨打电话的功能。

1.布局文件

android:hint="@string/phoneHint":表示隐藏的提示信息

android:inputType="phone":表示输入类型为电话号码

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity" >

android:id="@+id/etPhone"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="@string/phoneHint"

android:ems="10"

android:inputType="phone" >

android:id="@+id/btnCall"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/etPhone"

android:layout_below="@+id/etPhone"

android:text="@string/call" />

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

2.MainActivity

设置按钮监听事件

intent.setData(Uri.parse("tel:" + phoneNumber)):设置要传递的数据,Uri类型,电话号码要加前缀“tel:”

intent.setAction(Intent.ACTION_CALL):设置Intent的动作为打电话

在Windows系统中也有类似的功能,在命令行输入:start http://www.baidu.com,将用默认浏览器打开。

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

((Button)findViewById(R.id.btnCall)).setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

String phoneNumber =

((EditText) findViewById(R.id.etPhone)).getText().toString();

//意图:想干什么事

Intent intent = new Intent();

intent.setAction(Intent.ACTION_CALL);

//url:统一资源定位符

//uri:统一资源标示符(更广)

intent.setData(Uri.parse("tel:" + phoneNumber));

//开启系统拨号器

startActivity(intent);

}

});

}

}

3.报错,没有权限

运行时发现错误,

到LogCat中一看,发现如下错误:

03-22 16:58:47.263: E/AndroidRuntime(26347): java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxx cmp=com.android.phone/.OutgoingCallBroadcaster } from ProcessRecord{41eec660 26347:com.example.call/u0a77} (pid=26347, uid=10077) requires android.permission.CALL_PHONE

在AndroidManifest.xml中添加android.permission.CALL_PHONE权限即可

android:minSdkVersion="8"

android:targetSdkVersion="19" />

运行效果:

0818b9ca8b590ca3270a3433284dd417.png                    

0818b9ca8b590ca3270a3433284dd417.png

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值