软件编程
位置:首页>> 软件编程>> Android编程>> Android开发实现拨打电话与发送信息的方法分析

Android开发实现拨打电话与发送信息的方法分析

作者:xxiaowen  发布时间:2023-06-19 07:09:13 

标签:Android,拨打电话,发送信息

本文实例讲述了Android开发实现拨打电话与发送信息的方法。分享给大家供大家参考,具体如下:

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="短信内容"/>
    <EditText
      android:id="@+id/edit_main_content"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:hint="请输入短信内容"/>
  </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="发送信息"/>
  </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;
/**
* 拨打电话与发送信息
* @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){
       //点击拨打电话 创建一个Intent(隐式)
       //String action=Intent.ACTION_DIAL;
       //Intent intent=new Intent(action);
       Intent intent=new Intent(Intent.ACTION_DIAL);
       //携带数据
       String number=edit_main_number.getText().toString();
       intent.setData(Uri.parse("tel:"+number));
       //启动Activity
       startActivity(intent);
     }else if(v==btn_send){
       //点击发送信息  创建一个Intent(隐式)
       Intent intent=new Intent(Intent.ACTION_SENDTO);
       //携带数据
       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);
   //给视图对象设置点击监听
   btn_call.setOnClickListener(listener);
   btn_send.setOnClickListener(listener);
   //给视图对象设置长按监听
   btn_call.setOnLongClickListener(this);
   btn_send.setOnLongClickListener(this);
 }
 @Override
 public boolean onLongClick(View v) {
   if(v==btn_call){
    //长按拨打电话 创建一个Intent(隐式),必须在AndroidManifest.xml加入权限配置
    Intent intent=new Intent(Intent.ACTION_CALL);
    //携带数据
    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();
     //发送文本信息(短信)
     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加入权限配置


<uses-permission android:name="android.permission.CALL_PHONE"/><!-- 打电话的权限 -->
<uses-permission android:name="android.permission.SEND_SMS"/><!-- 发短信的权限 -->

另,关于Android权限设置可参考Android Manifest功能与权限描述大全

希望本文所述对大家Android程序设计有所帮助。

来源:http://blog.csdn.net/wenzhilanyu2012/article/details/50466029

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com