Android開發(fā)實現(xiàn)撥打電話與發(fā)送信息的方法分析
本文實例講述了Android開發(fā)實現(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="電話號碼" />
<EditText
android:id="@+id/edit_main_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="請輸入電話號碼"/>
</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="請輸入短信內(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){
//點擊撥打電話 創(chuàng)建一個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));
//啟動Activity
startActivity(intent);
}else if(v==btn_send){
//點擊發(fā)送信息 創(chuàng)建一個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);
//給視圖對象設(shè)置點擊監(jiān)聽
btn_call.setOnClickListener(listener);
btn_send.setOnClickListener(listener);
//給視圖對象設(shè)置長按監(jiān)聽
btn_call.setOnLongClickListener(this);
btn_send.setOnLongClickListener(this);
}
@Override
public boolean onLongClick(View v) {
if(v==btn_call){
//長按撥打電話 創(chuàng)建一個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));
//啟動Activity
startActivity(intent);
}else if(v==btn_send){
//得到SmsManager的對象
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ā)入門與進階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
android 使用瀏覽器打開指定頁面的實現(xiàn)方法
這篇文章主要介紹了android 使用瀏覽器打開指定頁面的實現(xiàn)方法,本文通過實例文字說明的形式給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06
Android studio無法創(chuàng)建類和接口和提示問題的完美解決辦法
這篇文章主要介紹了Android studio無法創(chuàng)建類和接口和提示問題解決辦法,內(nèi)容比較簡單,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-04-04
Android EditText實現(xiàn)關(guān)鍵詞批量搜索示例
本篇文章主要介紹了Android EditText實現(xiàn)關(guān)鍵詞批量搜索示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
實例講解Android中的AutoCompleteTextView自動補全組件
AutoCompleteTextView組件被用在輸入框中能實現(xiàn)輸入內(nèi)容自動補全的功能,類似于大家平時用Google時的輸入聯(lián)想,這里我們來用實例講解Android中的AutoCompleteTextView自動補全組件,特別是實現(xiàn)郵箱地址補全的例子,非常實用2016-05-05
深入理解TextView實現(xiàn)Rich Text--在同一個TextView設(shè)置不同字體風格
本篇文章是對Android中在同一個TextView中設(shè)置不同的字體風格進行了詳細的分析介紹,需要的朋友參考下2013-05-05
移動端開發(fā)之Jetpack?Hilt技術(shù)實現(xiàn)解耦
Hilt的出現(xiàn)解決前兩點問題,因為Hilt是Dagger針對Android平臺的場景化框架,比如Dagger需要我們手動聲明注入的地方,而Android聲明的地方不都在onCreate()嗎,所以Hilt就幫我們做了,除此之外還做了很多事情2023-02-02

