码迷,mamicode.com
首页 > 移动开发 > 详细

Android基础--电话拨号器

时间:2014-11-12 00:25:10      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   io   color   ar   os   java   

1.布局文件:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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="com.itheima.callphone.MainActivity" >

    <EditText
        android:id="@+id/et_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:ems="10" >
        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/bt_dial"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/et_number"
        android:text="@string/_dial" />

</RelativeLayout>

2.MainActivity.java

package com.itheima.callphone;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements OnClickListener {
    //用于输入电话号码的文本框
    private EditText et_number;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获得号码文本输入框
        et_number = (EditText)findViewById(R.id.et_number);
        //获得拨号按钮
        Button bt_dial = (Button)findViewById(R.id.bt_dial);
        bt_dial.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    //实现各种控件的点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.bt_dial:
            callPhone();
            break;

        default:
            break;
        }
    }
    //实现拨号操作
    private void callPhone() {
        //获取号码输入框的号码
        String number = et_number.getText().toString().trim();
        //进行非空判断
        if(TextUtils.isEmpty(number)){
            //给用户提示
            Toast.makeText(MainActivity.this, "号码不能为空", Toast.LENGTH_LONG).show();
        }
        //开启拨号服务
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:"+number));
        startActivity(intent);
    }
}

3.常见错误

错误描述:

java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxx-xxx-xxxx cmp=com.android.phone/.OutgoingCallBroadcaster } from ProcessRecord{b76dbe10 1322:com.itheima.callphone/10032} (pid=1322, uid=10032) requires android.permission.CALL_PHONE

原因:没有给应用程序添加拨号的权限,添加方法如下图.

bubuko.com,布布扣

bubuko.com,布布扣

Android基础--电话拨号器

标签:android   style   blog   http   io   color   ar   os   java   

原文地址:http://www.cnblogs.com/fujianyi/p/4090852.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!