Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 學習筆記(二):手把手實現電話撥號器

編輯:Android 開發實例

如今 Android 已經在智能手機市場佔據了強大的霸主地位,也吸引了越來越多的開發者。本文將以一個電話撥號器為例,帶領大家一步步完成一個簡單實用的 App。

一、新建項目 CallPhone

1.1 建立項目

二、設置界面與項目名稱

2.1 更改項目名稱

在 res/values 下的 strings.xml 中把 app_name 改為「電話拔號器」:

 <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">電話拔號器</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="dail">拔打電話</string> <string name="defaul_prop">請輸入電話號碼</string> </resources>

2.2 設置輸入框與按鈕

 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://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=".MainActivity" > <EditText android:id="@+id/et_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="phone" android:hint="@string/defaul_prop"> <requestFocus /> </EditText> <Button android:id="@+id/btn_dail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/et_number" android:text="@string/dail" /> </RelativeLayout>

三、編寫 Java 代碼

3.1 MainActivity.java(寫法一:內部監聽類)

 package com.pb.dial; 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; import android.app.Activity; import android.content.Intent; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt_dail= (Button) findViewById(R.id.btn_dail); bt_dail.setOnClickListener(new MyOnClickListener()); } private class MyOnClickListener implements OnClickListener{ @Override public void onClick(View v) { EditText et_number=(EditText) MainActivity.this.findViewById(R.id.et_number); String phoneNumber=et_number.getText().toString(); Intent intent=new Intent(); intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:"+phoneNumber)); startActivity(intent); } } }

3.1 MainActivity.java(寫法二:Activity 直接實現接口,並做空值校驗)

 package com.pb.dial; import android.net.Uri; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import android.app.Activity; import android.content.Intent; public class MainActivity extends Activity implements OnClickListener{ private Button bt_dail; private EditText et_number; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt_dail= (Button) findViewById(R.id.btn_dail); et_number=(EditText) MainActivity.this.findViewById(R.id.et_number); bt_dail.setOnClickListener(this); } @Override public void onClick(View v) { String phoneNumber=et_number.getText().toString().trim(); if(TextUtils.isEmpty(phoneNumber)){ Toast.makeText(this, "電話號碼不能為空", Toast.LENGTH_LONG).show(); return; } Intent intent=new Intent(); intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:"+phoneNumber)); startActivity(intent); } }

3.2 添加權限

由於要使用手機的撥號服務,必須在清單文件中加入 CALL_PHONE 權限:

 <!--添加權限 --> <uses-permission android:name="android.permission.CALL_PHONE"/>

3.3 運行效果

下面再額外分享一段代碼——同時包含「電話撥號器」和「短信發送器」的實現。

電話撥號器(Activity 部分)

 public class DialerAction extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { EditText editText = (EditText)findViewById(R.id.mobile); Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ editText.getText())); DialerAction.this.startActivity(intent); } }); } }

短信發送器

使用短信服務需要在清單文件中添加 SEND_SMS 權限:

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

Activity 主要代碼:

 String mobile = mobileView.getText().toString(); String content = contentView.getText().toString(); SmsManager smsManager = SmsManager.getDefault(); PendingIntent sentIntent = PendingIntent.getBroadcast(SMSSender.this, 0, new Intent(), 0); if(content.length()>70){//如果字數超過70,需拆分成多條短信發送 List<String> msgs = smsManager.divideMessage(content); for(String msg : msgs){ smsManager.sendTextMessage(mobile, null, msg, sentIntent, null); } }else{ smsManager.sendTextMessage(mobile, null, content, sentIntent, null); } Toast.makeText(SMSSender.this, "短信發送完成", Toast.LENGTH_LONG).show();

小結:撥號與發短信本質上都是通過 Intent 或系統服務調用硬件能力,關鍵是別忘了在清單文件中申請相應權限。需要注意的是,Intent.ACTION_CALL 會直接撥號,若只想打開撥號盤讓用戶確認,可改用 Intent.ACTION_DIAL,體驗上更安全友好。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved