Android開發(fā)實(shí)現(xiàn)撥打電話與發(fā)送信息的方法分析
本文實(shí)例講述了Android開發(fā)實(shí)現(xiàn)撥打電話與發(fā)送信息的方法。分享給大家供大家參考,具體如下:
xml布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="電話號(hào)碼" />
<EditText
android:id="@+id/edit_main_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="請(qǐng)輸入電話號(hào)碼"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="短信內(nèi)容"/>
<EditText
android:id="@+id/edit_main_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="請(qǐng)輸入短信內(nèi)容"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="撥打電話"/>
<Button
android:id="@+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="發(fā)送信息"/>
</LinearLayout>
</LinearLayout>
java代碼:
package com.wenzhi.interndemo;
import java.net.URL;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;
import android.content.Intent;
/**
* 撥打電話與發(fā)送信息
* @author xiaowen
* @2016-1-5 下午10:48:53
*/
public class ThreeActivity extends Activity implements OnLongClickListener {
private EditText edit_main_number;
private EditText edit_main_content;
private Button btn_call;
private Button btn_send;
private OnClickListener listener=new OnClickListener() {
@Override
public void onClick(View v) {
if(v==btn_call){
//點(diǎn)擊撥打電話 創(chuàng)建一個(gè)Intent(隱式)
//String action=Intent.ACTION_DIAL;
//Intent intent=new Intent(action);
Intent intent=new Intent(Intent.ACTION_DIAL);
//攜帶數(shù)據(jù)
String number=edit_main_number.getText().toString();
intent.setData(Uri.parse("tel:"+number));
//啟動(dòng)Activity
startActivity(intent);
}else if(v==btn_send){
//點(diǎn)擊發(fā)送信息 創(chuàng)建一個(gè)Intent(隱式)
Intent intent=new Intent(Intent.ACTION_SENDTO);
//攜帶數(shù)據(jù)
String number=edit_main_number.getText().toString();
String content=edit_main_content.getText().toString();
intent.setData(Uri.parse("smsto:"+number));
intent.putExtra("sms_body", content);
startActivity(intent);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three);
edit_main_number=(EditText) findViewById(R.id.edit_main_number);
edit_main_content=(EditText) findViewById(R.id.edit_main_content);
btn_call=(Button) findViewById(R.id.btn_call);
btn_send=(Button) findViewById(R.id.btn_send);
//給視圖對(duì)象設(shè)置點(diǎn)擊監(jiān)聽
btn_call.setOnClickListener(listener);
btn_send.setOnClickListener(listener);
//給視圖對(duì)象設(shè)置長按監(jiān)聽
btn_call.setOnLongClickListener(this);
btn_send.setOnLongClickListener(this);
}
@Override
public boolean onLongClick(View v) {
if(v==btn_call){
//長按撥打電話 創(chuàng)建一個(gè)Intent(隱式),必須在AndroidManifest.xml加入權(quán)限配置
Intent intent=new Intent(Intent.ACTION_CALL);
//攜帶數(shù)據(jù)
String number =edit_main_number.getText().toString();
intent.setData(Uri.parse("tel:"+number));
//啟動(dòng)Activity
startActivity(intent);
}else if(v==btn_send){
//得到SmsManager的對(duì)象
SmsManager smsManager=SmsManager.getDefault();
//發(fā)送文本信息(短信)
String number=edit_main_number.getText().toString();
String content=edit_main_content.getText().toString();
smsManager.sendTextMessage(number, null, content, null, null);
}
return true;
}
}
注意:在AndroidManifest.xml加入權(quán)限配置
<uses-permission android:name="android.permission.CALL_PHONE"/><!-- 打電話的權(quán)限 --> <uses-permission android:name="android.permission.SEND_SMS"/><!-- 發(fā)短信的權(quán)限 -->
另,關(guān)于Android權(quán)限設(shè)置可參考Android Manifest功能與權(quán)限描述大全
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
android 使用瀏覽器打開指定頁面的實(shí)現(xiàn)方法
這篇文章主要介紹了android 使用瀏覽器打開指定頁面的實(shí)現(xiàn)方法,本文通過實(shí)例文字說明的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
使用Flutter實(shí)現(xiàn)一個(gè)走馬燈布局的示例代碼
這篇文章主要介紹了使用 Flutter 實(shí)現(xiàn)一個(gè)走馬燈布局的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Android studio無法創(chuàng)建類和接口和提示問題的完美解決辦法
這篇文章主要介紹了Android studio無法創(chuàng)建類和接口和提示問題解決辦法,內(nèi)容比較簡單,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-04-04
Android EditText實(shí)現(xiàn)關(guān)鍵詞批量搜索示例
本篇文章主要介紹了Android EditText實(shí)現(xiàn)關(guān)鍵詞批量搜索示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
實(shí)例講解Android中的AutoCompleteTextView自動(dòng)補(bǔ)全組件
AutoCompleteTextView組件被用在輸入框中能實(shí)現(xiàn)輸入內(nèi)容自動(dòng)補(bǔ)全的功能,類似于大家平時(shí)用Google時(shí)的輸入聯(lián)想,這里我們來用實(shí)例講解Android中的AutoCompleteTextView自動(dòng)補(bǔ)全組件,特別是實(shí)現(xiàn)郵箱地址補(bǔ)全的例子,非常實(shí)用2016-05-05
深入理解TextView實(shí)現(xiàn)Rich Text--在同一個(gè)TextView設(shè)置不同字體風(fēng)格
本篇文章是對(duì)Android中在同一個(gè)TextView中設(shè)置不同的字體風(fēng)格進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
移動(dòng)端開發(fā)之Jetpack?Hilt技術(shù)實(shí)現(xiàn)解耦
Hilt的出現(xiàn)解決前兩點(diǎn)問題,因?yàn)镠ilt是Dagger針對(duì)Android平臺(tái)的場景化框架,比如Dagger需要我們手動(dòng)聲明注入的地方,而Android聲明的地方不都在onCreate()嗎,所以Hilt就幫我們做了,除此之外還做了很多事情2023-02-02
Android沉浸式狀態(tài)欄的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android沉浸式狀態(tài)欄的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09

