标签:
package com.example.demo;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private Button btnsend;
private EditText etContent;
private MyReciever receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnsend=(Button)findViewById(R.id.button1);
etContent=(EditText)findViewById(R.id.editText1);
btnsend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendSMS();
}
});
}
//TODO
protected void sendSMS() {
//系统短信发送信心
/*
*
*/
Intent intent=new Intent(Intent.ACTION_SENDTO);
Uri data=Uri.parse("smsto:"+5554);
intent.setData(data);
intent.putExtra("sms_body", etContent.getText());
startActivity(intent);
/*
* 发信息二
*/
// Intent intent01=new Intent(Intent.ACTION_SENDTO);
// intent01.setType(“vnd.android-dir/mms-sms”);
// intent01.putExtra(“address”, “155555251552”);
// intent01.putExtra(“sms_body”, etContent.getText().toString());
// startActivity(intent01);
// 2)利用代码发送短信,申请权限,利用代码发送短信不会写入系统的数据表
/*
* 短信的接收。android系统后台有一个服务(Service专门用来接收短消息,当有新的
*
*/
SmsManager manager=SmsManager.getDefault();
Intent intent02=new Intent(“com.tarena.SENT”);
PendingIntent pi1=PendingIntent.getBroadcast(this,0,intent02,0);
//系统某个时候会去调用系统个从自己手机发出了(到达了基站时候)就是pi1执行了
Intent intent03=new Intent(“com.tarena.DELIVERY”);
PendingIntent pi2=PendingIntent.getBroadcast(this,0, intent03, 0);
//代表信息发送到了对面手机收到的pi2
manager.sendTextMessage("151511211",null, "xihuanniyijinghenjiule",
pi1, pi2);
}
public class MyReciever extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action=intent.getAction();
if(("com.tarena.SENT").equals(action)){
Log.d("TAG", "短信发送成功了"+System.currentTimeMillis());
}
if("com.tarena.DELIVERY".equals(action)){
Log.d("TAG", "对方已经收到短信啦"+System.currentTimeMillis());
}
if("android.provider.Telephony.SMS_RECEIVED".equals(action)){
//处理短消息
Bundle bundle=intent.getExtras();
Object[] pdus=(Object[]) bundle.get("pdus");
StringBuilder sb=new StringBuilder();
String number="";
//把一个一个pdus转为一段段的短消息
for(int i=0;i<pdus.length;i++){
SmsMessage message=SmsMessage.createFromPdu((byte[]) pdus[i]);
sb.append(message.getDisplayMessageBody());
number=message.getDisplayOriginatingAddress();
}
Log.i("TAG", "发送方电话:"+number+"发送内容"+sb);
//有短信这条广播不再继续下发android.provider.Telephoney.sms_RECEIVED
/*
* 必须设定权限RECEiVE_SMS
* 设置较高的优先级
* 如果不希望系统的短信程序接收短信,就执行abortBroadcst广播就停止下发同时,
* 不会写入系统的数据库
*/
abortBroadcast();
}
}
}
@Override
protected void onResume() {
super.onResume();
receiver=new MyReciever();
IntentFilter filter=new IntentFilter();
filter.addAction("com.tarena.SENT");
filter.addAction("com.tarena.DELIVERY");
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
filter.setPriority(1001);
registerReceiver(receiver, filter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
}
权限的声明:
标签:
原文地址:http://blog.csdn.net/lyp13243189027/article/details/51355225