Android拨打电话与发送信息

代码如下

<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>

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"/><!-- 发短信的权限 -->


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值