行动不一定带来快乐,而无行动则决无快乐
本讲内容:接收和发送短信
示例一:接收短信
当手机接收到一条短信的时候,系统会发出一条值为android.provider.Telephony.SMS_RECEIVED的广播,这条广播里携带与短信相关的所有数据。
第一行显示短信的发送方号码,第二行显示短信的内容。当有短信到来时,短信的发送方号码和内容就显示在上面。
注意:真机接收不到android.provider.Telephony.SMS_RECEIVED广播
下面是res/layout/activity_main.xml 布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:padding="10dp" android:text="From:" android:textSize="20sp" /> <TextView android:id="@+id/sender" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:padding="10dp" android:text="Content:" android:textSize="20sp" /> <TextView android:id="@+id/content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textSize="20sp" /> </LinearLayout></LinearLayout>
下面是MainActivity.java主界面文件:
public class MainActivity extends Activity { private TextView sender;//显示短信的发送方 private TextView content;//显示短信的内容 private IntentFilter receiveFilter; private MessageReceiver messageReceiver; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sender=(TextView) findViewById(R.id.sender); content=(TextView) findViewById(R.id.content); receiveFilter=new IntentFilter(); receiveFilter.addAction("android.provider.Telephony.SMS_RECEIVED");//添加一個action messageReceiver=new MessageReceiver();// 创建一个广播接收器 registerReceiver(messageReceiver, receiveFilter);// 动态注册 } protected void onDestroy() { super.onDestroy(); unregisterReceiver(messageReceiver);// 取消注册 } class MessageReceiver extends BroadcastReceiver{ public void onReceive(Context context, Intent intent) { Bundle bundle=intent.getExtras();//获取intent里面的bundle对象 //因为传送bundle给它的代码里用了bundle.putString(“pdus”,xxxx);所以它只能用“pdus“的名字来取 Object[] pdus=(Object[]) bundle.get("pdus");//获取Bundle里面的数据 SmsMessage[] messages=new SmsMessage[pdus.length]; for(int i=0;i<messages.length;i++){ messages[i]=SmsMessage.createFromPdu((byte[]) pdus[i]); } String address=messages[0].getOriginatingAddress();//获取发送方号码 String fullMessage=""; for(SmsMessage message:messages){ fullMessage+=message.getMessageBody();//获取短信内容 } sender.setText(address); content.setText(fullMessage); } }}首先我们从Intent参数中取出一个Bundle对象,然后使用pdu密钥来提取一个SMS pdus数组,其中每一个pdu都表示一条短信消息。然后SmsMessage的crateFromPdu()方法将每一个pdu字节数组转换为SmsMessage对象,调用这个对象的getOriginatingAddress()方法就可以获取到短信的发送方号码,调用getMessageBody()方法就可以获取到短信的内容,然后将每一个SmsMessage对象中的短信内容拼接起来,就组成了一条完整的短信。
给程序声明一个接收短信的权限
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
示例二:拦截短信
下面是MainActivity.java主界面文件:
<span style="color:#333333;">public class MainActivity extends Activity { ..... protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sender=(TextView) findViewById(R.id.sender); content=(TextView) findViewById(R.id.content); receiveFilter=new IntentFilter(); receiveFilter.addAction("android.provider.Telephony.SMS_RECEIVED");//添加一個action </span><span style="color:#ff0000;">receiveFilter.setPriority(100);//提高MessageReceiver的優先級</span><span style="color:#333333;"> messageReceiver=new MessageReceiver();// 创建一个广播接收器 registerReceiver(messageReceiver, receiveFilter);// 动态注册 } ..... class MessageReceiver extends BroadcastReceiver{ public void onReceive(Context context, Intent intent) { <span style="white-space:pre"> </span> </span><span style="color:#ff0000;">abortBroadcast();//中止广播</span><span style="color:#333333;"> } }}</span>
下面是res/layout/activity_main.xml 布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:padding="10dp" android:text="To:" android:textSize="20sp" /> <EditText android:id="@+id/to" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" > <EditText android:id="@+id/centent" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1" android:textSize="20sp" /> <Button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="Send" /> </LinearLayout></LinearLayout>
下面是MainActivity.java主界面文件:
public class MainActivity extends Activity { private EditText to; private EditText content; private Button send; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); to = (EditText) findViewById(R.id.to); content = (EditText) findViewById(R.id.centent); send = (Button) findViewById(R.id.send); send.setOnClickListener(new OnClickListener() { public void onClick(View v) { SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(to.getText().toString(), null,content.getText().toString(), null, null); } }); }}首先调用SmsManager的getDefault()方法获取一个SmsManager对象,然后调用它的sendTextMessage()方法就可以发送短信了,该方法接收五个参数,第一个参数指定接收人的手机号码,第三个参数指定短信的内容,其它几个参数暂时用不到,传入null即可。
给程序声明一个发送短信的权限
<uses-permission android:name="android.permission.SEND_SMS"/>
示例四:
点击Send按钮虽然将短信发送出去了,但是我们并不知道到底发送成功没,这时候就可以利用sendTextMessage()方法的第四个参数来对短信的发送状态进行监控。
下面是MainActivity.java主界面文件:
public class MainActivity extends Activity { private EditText to; private EditText content; private Button send; private IntentFilter sendFilter; private SendStatusReceiver sendStatusReceiver; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); to = (EditText) findViewById(R.id.to); content = (EditText) findViewById(R.id.centent); send = (Button) findViewById(R.id.send); sendFilter=new IntentFilter(); sendFilter.addAction("SENT_SMS_ACTION"); sendStatusReceiver=new SendStatusReceiver(); registerReceiver(sendStatusReceiver, sendFilter); send.setOnClickListener(new OnClickListener() { public void onClick(View v) { SmsManager smsManager = SmsManager.getDefault(); Intent sentIntent=new Intent("SENT_SMS_ACTION"); PendingIntent pi=PendingIntent.getBroadcast(MainActivity.this, 0, sentIntent, 0); smsManager.sendTextMessage(to.getText().toString(), null,content.getText().toString(), pi, null); } }); } protected void onDestroy() { super.onDestroy(); unregisterReceiver(sendStatusReceiver); } //定義廣播監聽 class SendStatusReceiver extends BroadcastReceiver{ public void onReceive(Context context, Intent intent) { if(getResultCode()==RESULT_OK){ //短信發送成功 Toast.makeText(context, "Send succeeded", Toast.LENGTH_LONG).show(); }else{ Toast.makeText(context, "Send failed", Toast.LENGTH_LONG).show(); } } }}
Take your time and enjoy it 要原码的、路过的、学习过的请留个言,顶个呗~~