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", "发送短信");
}
} 布局样式
布局文件
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"/> 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" /> 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"/> 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" />