Loading

1-电话号拨号器

1>页面布局

<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"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/et_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="phone" />

    <Button
        android:id="@+id/bt_dail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/et_number"
        android:layout_below="@+id/et_number"
        android:text="@string/dail" />
        
</RelativeLayout>


2>MainActivity

package com.dail;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button but = (Button)findViewById(R.id.bt_dail);
        but.setOnClickListener(new myListener());
    }

    private class myListener implements OnClickListener
    {

        @Override
        public void onClick(View v) {
            EditText et = (EditText)findViewById(R.id.et_number);
            String number = et.getText().toString();
            
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:"+number));
            
            startActivity(intent);
        }
        
        
    }
}
View Code

3>清单文件AndroidManifest.xml 添加打电话权限

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

如果不加权限会报错

权限安全异常:
java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:110 cmp=com.android.phone/.OutgoingCallBroadcaster } from ProcessRecord{43e96760 663:com.dail/10038} (pid=663, uid=10038) requires android.permission.CALL_PHONE

 

 

 

posted @ 2013-12-07 16:31  The Mechanic  阅读(132)  评论(0编辑  收藏  举报