android項目1:打電話,android項目打電話
android項目1:打電話
一、
二、步驟
1、畫好主介面
/call/res/layout/activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <EditText 8 android:id="@+id/editText_phoneNumber" 9 android:layout_width="match_parent"10 android:layout_height="wrap_content"11 android:layout_weight="0.00"12 android:ems="10" >13 14 <requestFocus />15 </EditText>16 17 <Button18 android:id="@+id/btn_call"19 android:layout_width="match_parent"20 android:layout_height="50dp"21 android:layout_weight="0.00"22 android:text="@string/btn_call" />23 24 </LinearLayout>
2、編好代碼
com.fry.call_1.MainActivity
1 package com.fry.call_1; 2 3 4 5 6 7 import android.app.Activity; 8 import android.content.Intent; 9 import android.net.Uri;10 import android.os.Bundle;11 import android.view.View;12 import android.view.View.OnClickListener;13 import android.widget.Button;14 import android.widget.EditText;15 16 17 18 public class MainActivity extends Activity{19 private Button btn_call;//建立一個button對象20 private EditText editText_phoneNumber;21 protected void onCreate(Bundle savedInstanceState) {22 super.onCreate(savedInstanceState);//父類操作23 setContentView(R.layout.activity_main);//引入名為activity_main的介面24 btn_call=(Button) findViewById(R.id.btn_call);//找id為btn_openActivity的button25 editText_phoneNumber=(EditText) findViewById(R.id.editText_phoneNumber);26 27 //1、給按鈕設定點擊事件28 btn_call.setOnClickListener(new OnClickListener() {//設定button點擊監聽29 30 @Override31 public void onClick(View v) {//onclick事件32 // TODO Auto-generated method stub33 //2、拿到編輯框中的號碼34 String phoneNumber=editText_phoneNumber.getText().toString();35 //3、給這個號碼打電話36 37 Intent intent=new Intent();//初始化intent38 intent.setAction("android.intent.action.CALL");39 intent.addCategory("android.intent.category.DEFAULT");40 intent.setData(Uri.parse("tel:"+phoneNumber));41 startActivity(intent);//開啟activity42 }43 });44 }45 }
3、設定好許可權
/call/AndroidManifest.xml
1 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 2 package="com.fry.call_1" 3 android:versionCode="1" 4 android:versionName="1.0" > 5 6 <uses-permission android:name="android.permission.CALL_PHONE" /> 7 8 <uses-sdk 9 android:minSdkVersion="8"10 android:targetSdkVersion="19" />11 12 <application13 android:allowBackup="true"14 android:icon="@drawable/ic_launcher"15 android:label="@string/app_name"16 android:theme="@style/AppTheme" >17 <activity18 android:name="com.fry.call_1.MainActivity"19 android:label="@string/app_name" >20 <intent-filter>21 <action android:name="android.intent.action.MAIN" />22 23 <category android:name="android.intent.category.LAUNCHER" />24 </intent-filter>25 </activity>26 <activity android:name="com.fry.call_1.Activity01" android:exported="true"></activity>27 </application>28 29 </manifest>