歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android模擬打電話應用程序實現分析

Android模擬打電話應用程序實現分析

日期:2017/3/1 10:46:28   编辑:Linux編程

1、設計界面

今天學了一個小程序,實現簡單的打電話功能。下面我來解析一下怎麼來完成打電話的功能。

一、設計頁面

二、Activity的開發

1. 我們先創建一個Android工程Phone

2. 在res文件下的values目錄下的strings.xml中寫入數據:

<string name=”input_info”>請輸入電話號碼</string>

<string name=”dial_caption”>撥打</string>

實現Android的數據傳遞。

3. 在layout中實現頁面的布局,在main.xml中寫:

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

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

<!-定義文本框 -->

<EditText

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/phone_number"/>

<!-定義一個按鈕 -->

<Button

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/dial_caption"

android:id="@+id/dial_btn" />

4. 在PhoneActivity.java中寫代碼:

package cn.csdn.android;

import android.app.Activity;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

public class PhoneActivity extends Activity {

/** Called when the activity is first created. */

EditText numberEt;

Button dialBtn;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

findViews();

dialBtn.setOnClickListener(new OnClickListener(){

public void onClick(View v) {

//調用系統的撥號服務實現電話撥打功能

String phone_number=numberEt.getText().toString();

phone_number=phone_number.trim();

if(phone_number !=null && !phone_number.equals("")){

//封裝一個撥打電話的intent,並且將電話號碼包裝成一個Uri對象傳入

Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phone_number));

PhoneActivity.this.startActivity(intent);

}

}

});

}

public void findViews(){

numberEt=(EditText) this.findViewById(R.id.phone_number);

dialBtn=(Button) this.findViewById(R.id.dial_btn);

}

}

Copyright © Linux教程網 All Rights Reserved