Android學(xué)習(xí)筆記(二)之電話撥號(hào)器
目前Android已經(jīng)在只能手機(jī)市場(chǎng)已經(jīng)具有強(qiáng)大的霸主地位,也吸引了越來越多的追捧者。Android的學(xué)習(xí)也越來越火。但是,報(bào)名費(fèi)用確實(shí)大多人望而卻步
一、新建項(xiàng)目CallPhone
1.1、建立項(xiàng)目

二、設(shè)置界面與項(xiàng)目名稱
2.1、更改項(xiàng)目名稱
res/values下strings.xml中更改app_name電話拔號(hào)器
string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">電話拔號(hào)器</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="dail">拔打電話</string> <string name="defaul_prop">請(qǐng)輸入電話號(hào)碼</string> </resources>
2.2、設(shè)置文件框與按鍵
<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"
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);
//根據(jù)ID找到ID,拔號(hào)按鍵的ID
Button bt_dail= (Button) findViewById(R.id.btn_dail);
//為按鍵設(shè)置點(diǎn)擊事件
bt_dail.setOnClickListener(new MyOnClickListener());
}
//單擊監(jiān)聽事件
private class MyOnClickListener implements OnClickListener{
/**
* 單擊按鍵被點(diǎn)擊時(shí)調(diào)用的方法
*/
@Override
public void onClick(View v) {
//取出輸入框中的內(nèi)容
//先找到ID
EditText et_number=(EditText) MainActivity.this.findViewById(R.id.et_number);
//根據(jù)ID出內(nèi)容
String phoneNumber=et_number.getText().toString();
//意圖,想做什么事
Intent intent=new Intent();
//開始拔打電話
intent.setAction(Intent.ACTION_CALL);
//設(shè)置動(dòng)作內(nèi)容 uri:統(tǒng)一資源標(biāo)識(shí)符,url的類型 統(tǒng)一資源定位符
intent.setData(Uri.parse("tel:"+phoneNumber));
//開啟新的界面
startActivity(intent);
}
}
}
或者
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);
//根據(jù)ID找到ID,拔號(hào)按鍵的ID
bt_dail= (Button) findViewById(R.id.btn_dail);
//取出輸入框中的內(nèi)容
//先找到ID
//根據(jù)ID出內(nèi)容
et_number=(EditText) MainActivity.this.findViewById(R.id.et_number);
//為按鍵設(shè)置點(diǎn)擊事件
bt_dail.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String phoneNumber=et_number.getText().toString().trim();
//判斷內(nèi)容是否為空 TextUtils是個(gè)工具類
if(TextUtils.isEmpty(phoneNumber)){
Toast.makeText(this, "電話號(hào)碼不能為空", Toast.LENGTH_LONG).show();
//如果是內(nèi)容類請(qǐng)將this改為MainActivity.this
return;
}
//意圖,想做什么事
Intent intent=new Intent();
//開始拔打電話
intent.setAction(Intent.ACTION_CALL);
//設(shè)置動(dòng)作內(nèi)容 uri:統(tǒng)一資源標(biāo)識(shí)符,url的類型 統(tǒng)一資源定位符
intent.setData(Uri.parse("tel:"+phoneNumber));
//開啟新的界面
startActivity(intent);
}
//單擊監(jiān)聽事件
/* private class MyOnClickListener implements OnClickListener{
*//**
* 單擊按鍵被點(diǎn)擊時(shí)調(diào)用的方法
*//*
@Override
public void onClick(View v) {
//取出輸入框中的內(nèi)容
//先找到ID
EditText et_number=(EditText) MainActivity.this.findViewById(R.id.et_number);
//根據(jù)ID出內(nèi)容
String phoneNumber=et_number.getText().toString();
//意圖,想做什么事
Intent intent=new Intent();
//開始拔打電話
intent.setAction(Intent.ACTION_CALL);
//設(shè)置動(dòng)作內(nèi)容 uri:統(tǒng)一資源標(biāo)識(shí)符,url的類型 統(tǒng)一資源定位符
intent.setData(Uri.parse("tel:"+phoneNumber));
//開啟新的界面
startActivity(intent);
}
}*/
}
3.2、添加權(quán)限
<!--添加權(quán)限 --> <uses-permission android:name="android.permission.CALL_PHONE"/>
3.3、運(yùn)行

下面給大家分享一段代碼 ————android電話拔號(hào)器和短信發(fā)送器
android電話拔號(hào)器
因?yàn)閼?yīng)用要使用手機(jī)的電話服務(wù),所以要在清單文件AndroidManifest.xml中添加電話服務(wù)權(quán)限:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.itcast.action" android:versionCode="1" android:versionName="1.0"> 略.... <uses-sdk android:minSdkVersion=“6" /> <uses-permission android:name="android.permission.CALL_PHONE"/> </manifest>
界面布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/inputmobile"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/mobile"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:id="@+id/button"/> </LinearLayout>
LinearLayout (線性布局)、AbsoluteLayout(絕對(duì)布局)、RelativeLayout(相對(duì)布局)、TableLayout(表格布局)、FrameLayout(幀布局)
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);
}
});
}
}
短信發(fā)送器
因?yàn)閼?yīng)用要使用手機(jī)的短信服務(wù),所以要在清單文件AndroidManifest.xml中添加短信服務(wù)權(quán)限:
<uses-permission android:name="android.permission.SEND_SMS"/>
界面布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical“ android:layout_width="fill_parent“ android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/inputmobile"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/mobile"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/content"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:minLines="3" android:id="@+id/content"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:id="@+id/button"/> </LinearLayout>
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){//如果字?jǐn)?shù)超過70,需拆分成多條短信發(fā)送
List<String> msgs = smsManager.divideMessage(content);
for(String msg : msgs){
smsManager.sendTextMessage(mobile, null, msg, sentIntent, null);
//最后二個(gè)參數(shù)為短信已發(fā)送的廣播意圖,最后一個(gè)參數(shù)為短信對(duì)方已收到短信的廣播意圖
}
}else{
smsManager.sendTextMessage(mobile, null, content, sentIntent, null);
}
Toast.makeText(SMSSender.this, "短信發(fā)送完成", Toast.LENGTH_LONG).show();
相關(guān)文章
Android實(shí)現(xiàn)滾動(dòng)刻度尺效果
本篇文章主要介紹了Android實(shí)現(xiàn)滾動(dòng)刻度尺效果,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
Android 錄制手機(jī)屏幕視頻生成GIF圖片實(shí)例詳解
這篇文章主要介紹了Android 錄制手機(jī)屏幕視頻生成GIF圖片實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android 實(shí)現(xiàn)手機(jī)撥打電話的功能
本篇文章主要介紹 Android 開發(fā)手機(jī)撥打電話的功能,這里提供示例代碼,有興趣的小伙伴可以參考下2016-08-08
Android開發(fā)實(shí)現(xiàn)文件存儲(chǔ)功能
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)實(shí)現(xiàn)文件存儲(chǔ)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
Android上實(shí)現(xiàn)easyconfig(airkiss)方法
本篇文章主要給大家講解了在Android系統(tǒng)上實(shí)現(xiàn)easyconfig(airkiss)的方法,有這方面需要的朋友參考學(xué)習(xí)下吧。2018-01-01
在Android Studio中設(shè)置Button透明度的方法詳解
本文將介紹在Android Studio中如何設(shè)置Button的透明度,首先,我們將展示實(shí)現(xiàn)該功能的整個(gè)流程,并使用表格列出每個(gè)步驟,然后,我們將詳細(xì)說明每個(gè)步驟需要做什么,并提供相應(yīng)的代碼和注釋,需要的朋友可以參考下2023-09-09
Android Dialog對(duì)話框?qū)嵗a講解
本文通過實(shí)例代碼給大家介紹了Android Dialog對(duì)話框的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08

