溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android開發(fā)實(shí)現(xiàn)撥打電話與發(fā)送信息的方法分析

發(fā)布時(shí)間:2020-10-16 00:29:22 來源:腳本之家 閱讀:159 作者:xxiaowen 欄目:移動(dòng)開發(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="電話號碼" />
    <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){
        //點(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);
    //給視圖對象設(shè)置點(diǎn)擊監(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)建一個(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的對象
      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é)》

希望本文所述對大家Android程序設(shè)計(jì)有所幫助。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI