标签:android style blog java color os
接到Boss的命令,要求我们做一个在Android手机上运行的小的例子,实现的功能是:通过一个手机向另外一个手机发送数据,另外一个Android手机接收到后通过USB接口输出!
该装置主要由三个步骤组成:
1、发信息
2、接收信息并读取信息内容
3、从 USB 口输出(这里暂时没实现)
就是为了实现一个短信发送,并接收短信内容输出的装置
MainActivity.java 类实现的是发送信息的功能
1 package com.example.android_phone; 2 3 import java.util.ArrayList; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.telephony.SmsManager; 8 import android.view.Menu; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.widget.Button; 12 import android.widget.Toast; 13 14 public class MainActivity extends Activity { 15 private Button mButton; 16 private String mContent; 17 private String mPhoneNumber; 18 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 mButton = (Button) this.findViewById(R.id.button1); 24 mContent = "短信内容"; 25 mPhoneNumber = "1833607****"; 26 mButton.setOnClickListener(new OnClickListener() { 27 @Override 28 public void onClick(View v) { 29 // TODO Auto-generated method stub 30 send(mPhoneNumber,mContent); 31 } 32 }); 33 } 34 35 private void send(String phoneNumber, String content){ 36 SmsManager smsManager =SmsManager.getDefault(); 37 ArrayList<String> texts = smsManager.divideMessage(content); 38 for(String text : texts){ 39 smsManager.sendTextMessage(phoneNumber, null, text, null, null); 40 } 41 Toast.makeText(getApplicationContext(), "---发送成功---", Toast.LENGTH_LONG).show(); 42 } 43 44 }
SMSBroadcastReceiver.java 类实现对广播的监听,并读取短信的内容
1 package com.example.android_phone; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 import android.content.BroadcastReceiver; 7 import android.content.Context; 8 import android.content.Intent; 9 import android.os.Bundle; 10 import android.telephony.SmsMessage; 11 import android.widget.Toast; 12 13 public class SMSBroadcastReceiver extends BroadcastReceiver{ 14 15 //信息接收广播 16 private static final Object ACTION = "android.provider.Telephony.SMS_RECEIVED"; 17 private String sender; 18 private String content; 19 private String sendtime; 20 @Override 21 public void onReceive(Context context, Intent intent) { 22 // TODO Auto-generated method stub 23 if (intent.getAction().equals(ACTION)) { 24 StringBuffer SMSAddress = new StringBuffer(); 25 StringBuffer SMSContent = new StringBuffer(); 26 Bundle bundle = intent.getExtras(); 27 if (bundle != null) { 28 // 通过pdus获得接收到的所有短信消息,获取短信内容; 29 Object[] pdusObjects = (Object[]) bundle.get("pdus"); 30 // 构建短信对象数组; 31 SmsMessage[] messages = new SmsMessage[pdusObjects.length]; 32 for (int i = 0; i < pdusObjects.length; i++) { 33 // 获取单条短信内容,以pdu格式存,并生成短信对象; 34 messages[i] = SmsMessage 35 .createFromPdu((byte[]) pdusObjects[i]); 36 } 37 for (SmsMessage message : messages) { 38 SMSAddress.append(message.getDisplayOriginatingAddress()); 39 SMSContent.append(message.getDisplayMessageBody()); 40 41 42 SMSAddress.append("短信来自:" + message.getDisplayOriginatingAddress() 43 + "\n"); 44 SMSContent.append("短信内容:" + message.getMessageBody()); 45 46 sender = message.getDisplayOriginatingAddress();// 获取短信的发送者 47 content = message.getMessageBody();// 获取短信的内容 48 Date date = new Date(message.getTimestampMillis()); 49 SimpleDateFormat format = new SimpleDateFormat( 50 "yyyy-MM-dd HH:mm:ss"); 51 sendtime = format.format(date);// 获取短信发送时间; 52 // SmsManager manager = SmsManager.getDefault(); 53 // manager.sendTextMessage("5556", 54 // null,"发送人:"+sender+"-----发送时间:"+sendtime+"----内容:"+content, 55 // null, null);//把拦截到的短信发送到指定的手机,此处为5556; 56 // if ("5556".equals(sender)){ 57 // //屏蔽手机号为5556的短信,这里还可以时行一些处理,如把该信息发送到第三人的手机等等。 58 // abortBroadcast(); 59 // } 60 Toast.makeText(context, "短信内容:"+content, Toast.LENGTH_LONG).show(); 61 } 62 63 } 64 } 65 } 66 67 }
权限设置
1 <!-- 发送短信 --> 2 <uses-permission android:name="android.permission.SEND_SMS"/> 3 <!-- 接收短信 --> 4 <uses-permission android:name="android.permission.RECEIVE_SMS"/> 5 <!-- 读取短信内容 --> 6 <uses-permission android:name="android.permission.READ_SMS"/> 7 <!-- 收到短信时广播 --> 8 <uses-permission android:name="android.permission.BROADCAST_SMS"/> 9 <!-- 编写短信--> 10 <uses-permission android:name="android.permission.WRITE_SMS"/> 11
广播监听
1 <receiver android:name=".SMSBroadcastReceiver"> 2 <intent-filter android:priority="800"> 3 <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 4 </intent-filter> 5 </receiver>
---------------------------------------未完待续---------------------------------------
标签:android style blog java color os
原文地址:http://www.cnblogs.com/zhjsll/p/3861532.html