Android第一次项目-拨打电话

来源:互联网 发布:想开个淘宝刷单工作室 编辑:程序博客网 时间:2024/05/18 02:46

AndroidManifest.xml配置


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.example.test"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="17" />
    <!--新增电话的权限  --><uses-permission android:name="android.permission.CALL_PHONE" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="cn.leaf.test.RealActivity"
            android:label="@string/call_act" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>



java文件


package cn.leaf.test;


import cn.example.test.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class RealActivity  extends Activity {
//声明两个组件
private Button btn;
private EditText no;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置布局文件
setContentView(R.layout.call);
//在setContentView之后,获取这两个元素
btn = (Button) findViewById(R.id.bt_call);
no = (EditText)findViewById(R.id.et_call);
//给btn添加事件
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// 获取号码
String phoneNo = no.getText().toString();
if (phoneNo.equals("")) {
Toast.makeText(RealActivity.this, "请输入号码你大爷的!",
Toast.LENGTH_LONG).show();
return;
} else {
// 去打电话。。
Intent intent = new Intent();
// 使用意图的名称
intent.setAction(Intent.ACTION_CALL);
String data = "tel:" + phoneNo;
// 设置数据
intent.setData(Uri.parse(data));
// 启动打电话功能
startActivity(intent);
}
}
});
}
}



Activity



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <EditText 
        android:id="@+id/et_call"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/et_call"
        />
   <Button 
       android:id="@+id/bt_call"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/bt_call"
       />
</LinearLayout>



注意点:1.  AndroidManifest.xml配置上主activity的入口   2.配置拨打电话的权限   3.java文件中的activity不要配置错误了






1 0