亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2017年12月05日 10:42:45   作者:xxiaowen  
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)撥打電話與發(fā)送信息的方法,結(jié)合實(shí)例形式分析了Android撥打電話及發(fā)送信息相關(guān)布局、功能實(shí)現(xiàn)及權(quán)限控制操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Android開(kāi)發(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)聽(tīng)
    btn_call.setOnClickListener(listener);
    btn_send.setOnClickListener(listener);
    //給視圖對(duì)象設(shè)置長(zhǎng)按監(jiān)聽(tīng)
    btn_call.setOnLongClickListener(this);
    btn_send.setOnLongClickListener(this);
  }
  @Override
  public boolean onLongClick(View v) {
    if(v==btn_call){
     //長(zhǎng)按撥打電話 創(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開(kāi)發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)

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

相關(guān)文章

最新評(píng)論