打电话和发送短信(带有发送是否成功,和对方是否接收成功)

package com.zhuoxin.day01_call;


import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity implements OnClickListener {


EditText et, et_msg;
Button btn, btn_send;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.et_num);
et_msg = (EditText) findViewById(R.id.et_msg);
btn = (Button) findViewById(R.id.btn_call);
btn_send = (Button) findViewById(R.id.btn_sendmsg);
btn.setOnClickListener(this);
btn_send.setOnClickListener(this);
}


public PendingIntent sendState() {
String SENT_SMS_ACTION = "SENT_SMS_ACTION";
Intent sentIntent = new Intent(SENT_SMS_ACTION);
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, sentIntent,
0);
//注册广播
this.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context _context, Intent _intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(MainActivity.this, "短信发送成功",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(MainActivity.this, "短信发送失败",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(MainActivity.this, "短信发送失败",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(MainActivity.this, "短信发送失败",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT_SMS_ACTION));
return sentPI;
}


public PendingIntent getState() {
// 返回是否成功
String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";

Intent getIntent = new Intent(DELIVERED_SMS_ACTION);
PendingIntent getPI = PendingIntent.getBroadcast(this, 0,
getIntent, 0);
this.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context _context, Intent _intent) {
Toast.makeText(MainActivity.this, "收信人已经成功接收",
Toast.LENGTH_SHORT).show();
}
}, new IntentFilter(DELIVERED_SMS_ACTION));
return getPI;
}


@Override
public void onClick(View v) {
PendingIntent sendPI = sendState();
PendingIntent getPI = getState();


// v.getId() 获取点击按钮时的id值
if (v.getId() == R.id.btn_call) {
// System.out.println("按钮被点击了");
// Log.d("onclick", "按钮被点击了");


// context, (上下文对象)在哪个activity中显示
// text, 要显示的文字
// duration 显示多少时间Toast.LENGTH_SHORT,Toast.LENGTH_LONG


// 吐丝
Toast.makeText(this, "按钮被点击了", Toast.LENGTH_LONG).show();


// 意图
Intent intent = new Intent();
// 设置想要做的事情 Intent.ACTION_CALL=android.intent.action.CALL;
intent.setAction(Intent.ACTION_CALL);
// 设置电话号码
intent.setData(Uri.parse("tel:" + et.getText()));
// 启动这个意图
startActivity(intent);
} else if (v.getId() == R.id.btn_sendmsg) {
String msg = et_msg.getText() + "";
if ("".equals(msg)) {
Toast.makeText(this, "请输入短信内容", Toast.LENGTH_SHORT).show();
return;
} else {
doSendMsg(et.getText() + "", msg, sendPI,getPI);
}


}


}


public void doSendMsg(String phonNum, String msg, PendingIntent pendIntent,
PendingIntent deliverPI) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phonNum, null, msg, pendIntent, deliverPI);
Log.i("onclick", "发送短信");
}


}布局样式



布局文件


<EditText 
        android:id="@+id/et_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:drawable/edit_text"
        android:hint="请输入电话号码"
        android:ems="10"/>
     
     
     
    <!-- layout_alignLeft  与哪个控件左对齐 
    android:layout_below 在哪个控件下边
    android:layout_above 在哪个控件的上边
    -->
    <Button 
         android:id="@+id/btn_call"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="打电话"
         android:layout_alignLeft="@id/et_num"
         android:layout_below="@id/et_num"
        />
    <EditText 
        android:id="@+id/et_msg"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:layout_alignLeft="@id/et_num"
        android:layout_below="@id/btn_call"
        android:background="@android:drawable/edit_text"
        android:gravity="top|left"
        android:hint="请输入短信内容"
        android:ems="10"/>
     <requestFocus />
     <Button 
         android:id="@+id/btn_sendmsg"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="发短信"
         android:layout_alignLeft="@id/et_num"
         android:layout_below="@id/et_msg"
        />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值