天天看點

Android 讀取、接收、發送 手機短信

1、Android 讀取手機短信

​擷取 android 手機短信需要在 AndroidManifest.xml 權重限:

<uses-permission android:name="android.permission.READ_SMS" />      

擷取短信隻需要得到 ContentResolver 就行了,它的 URI 主要有:

content://sms/          所有短信
content://sms/inbox     收件箱
content://sms/sent      已發送
content://sms/draft     草稿
content://sms/outbox    發件箱
content://sms/failed    發送失敗
content://sms/queued    待發送清單      

SMS 資料庫中的字段如下:

_id        一個自增字段,從1開始
thread_id  序号,同一發信人的id相同
address    發件人手機号碼
person     聯系人清單裡的序号,陌生人為null
date       發件日期
protocol   協定,分為: 0 SMS_RPOTO, 1 MMS_PROTO
read       是否閱讀 0未讀, 1已讀
status     狀态 -1接收,0 complete, 64 pending, 128 failed
type       ALL = 0;INBOX = 1;SENT = 2;DRAFT = 3;OUTBOX = 4;FAILED = 5; QUEUED = 6;
body       短信内容
service_center 短信服務中心号碼編号。如+8613800755500
subject        短信的主題
reply_path_present TP-Reply-Path
locked      

示例代碼:

package com.lmy.sms;

import java.sql.Date;
import java.text.SimpleDateFormat;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ScrollView;
import android.widget.TextView;

public class SmsReadActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView tv = new TextView(this);
        tv.setText(getSmsInPhone());

        ScrollView sv = new ScrollView(this);
        sv.addView(tv);

        setContentView(sv);
    }

    public String getSmsInPhone() {
        final String SMS_URI_ALL = "content://sms/"; // 所有短信
        final String SMS_URI_INBOX = "content://sms/inbox"; // 收件箱
        final String SMS_URI_SEND = "content://sms/sent"; // 已發送
        final String SMS_URI_DRAFT = "content://sms/draft"; // 草稿
        final String SMS_URI_OUTBOX = "content://sms/outbox"; // 發件箱
        final String SMS_URI_FAILED = "content://sms/failed"; // 發送失敗
        final String SMS_URI_QUEUED = "content://sms/queued"; // 待發送清單

        StringBuilder smsBuilder = new StringBuilder();

        try {
            Uri uri = Uri.parse(SMS_URI_ALL);
            String[] projection = new String[] { "_id", "address", "person",
                    "body", "date", "type", };
            Cursor cur = getContentResolver().query(uri, projection, null,
                    null, "date desc"); // 擷取手機内部短信
            // 擷取短信中最新的未讀短信
            // Cursor cur = getContentResolver().query(uri, projection,
            // "read = ?", new String[]{"0"}, "date desc");
            if (cur.moveToFirst()) {
                int index_Address = cur.getColumnIndex("address");
                int index_Person = cur.getColumnIndex("person");
                int index_Body = cur.getColumnIndex("body");
                int index_Date = cur.getColumnIndex("date");
                int index_Type = cur.getColumnIndex("type");

                do {
                    String strAddress = cur.getString(index_Address);
                    int intPerson = cur.getInt(index_Person);
                    String strbody = cur.getString(index_Body);
                    long longDate = cur.getLong(index_Date);
                    int intType = cur.getInt(index_Type);

                    SimpleDateFormat dateFormat = new SimpleDateFormat(
                            "yyyy-MM-dd hh:mm:ss");
                    Date d = new Date(longDate);
                    String strDate = dateFormat.format(d);

                    String strType = "";
                    if (intType == 1) {
                        strType = "接收";
                    } else if (intType == 2) {
                        strType = "發送";
                    } else if (intType == 3) {
                        strType = "草稿";
                    } else if (intType == 4) {
                        strType = "發件箱";
                    } else if (intType == 5) {
                        strType = "發送失敗";
                    } else if (intType == 6) {
                        strType = "待發送清單";
                    } else if (intType == 0) {
                        strType = "是以短信";
                    } else {
                        strType = "null";
                    }

                    smsBuilder.append("[ ");
                    smsBuilder.append(strAddress + ", ");
                    smsBuilder.append(intPerson + ", ");
                    smsBuilder.append(strbody + ", ");
                    smsBuilder.append(strDate + ", ");
                    smsBuilder.append(strType);
                    smsBuilder.append(" ]\n\n");
                } while (cur.moveToNext());

                if (!cur.isClosed()) {
                    cur.close();
                    cur = null;
                }
            } else {
                smsBuilder.append("no result!");
            }

            smsBuilder.append("getSmsInPhone has executed!");

        } catch (SQLiteException ex) {
            Log.d("SQLiteException in getSmsInPhone", ex.getMessage());
        }

        return smsBuilder.toString();
    }
}      

2、Android 接收短信

啟動程式時啟動一個 service,在 service 裡注冊接收短信的廣播,當手機收到短信裡,列印出短信内容跟電話号碼。

package com.lmy.SmsListener;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SmsListenerActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.main);
        TextView tv = new TextView(this);
        tv.setText("Hello. I started!");
        setContentView(tv);
        Intent service = new Intent(this, MyService.class);
        this.startService(service);
    }
}      

當 service 被 kill 後,我們可以在開機時自動啟動 service。

開機自動啟動一個 service,在 service 裡注冊接收短信的廣播,當手機收到短信裡,列印出短信内容跟電話号碼。

開機啟動後系統會發出一個 Standard Broadcast Action,名字叫android.intent.action.BOOT_COMPLETED,這個 Action 隻會發出一次。

建立一個類繼承 BroadcastReceiver,在 onReceive(Context context, Intent intent) 裡面啟動service。

package com.lmy.SmsListener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyBrocast extends BroadcastReceiver {

    static final String ACTION = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v("dimos", "MyBrocast");
        if (intent.getAction().equals(ACTION)) {
            Intent service = new Intent(context, MyService.class);
            context.startService(service);
        }
    }

}      

在 service 中注冊一個接收短信的廣播:

package com.lmy.SmsListener;

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {

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

    @Override
    public void onCreate() {
        super.onCreate();
        IntentFilter localIntentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
        localIntentFilter.setPriority(2147483647);
        SmsRecevier localMessageReceiver = new SmsRecevier();
        Log.v("dimos", "MyService");
        registerReceiver(localMessageReceiver, localIntentFilter);
    }
    

}      

廣播接收到短信:

package com.lmy.SmsListener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class SmsRecevier extends BroadcastReceiver {

    public SmsRecevier() {
        super();
        Log.v("dimos", "SmsRecevier create");
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        String dString = SmsHelper.getSmsBody(intent);
        String address = SmsHelper.getSmsAddress(intent);
        Log.i("dimos", dString+","+address);
        //阻止廣播繼續傳遞,如果該receiver比系統的級别高,
        //那麼系統就不會收到短信通知了

        abortBroadcast(); 
    }
}      

獲得短信内容跟短信位址:

package com.lmy.SmsListener;

import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

public class SmsHelper {
    /**
     * 獲得短信内容
     * */
    public static String getSmsBody(Intent intent) {

        String tempString = "";
        Bundle bundle = intent.getExtras();
        Object messages[] = (Object[]) bundle.get("pdus");
        SmsMessage[] smsMessage = new SmsMessage[messages.length];
        for (int n = 0; n < messages.length; n++) {
            smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
            // 短信有可能因為使用了回車而導緻分為多條,是以要加起來接受
            tempString += smsMessage[n].getDisplayMessageBody();
        }
        return tempString;

    }

    /**
     * 獲得短信位址
     * */
    public static String getSmsAddress(Intent intent) {

        Bundle bundle = intent.getExtras();
        Object messages[] = (Object[]) bundle.get("pdus");
        return SmsMessage.createFromPdu((byte[]) messages[0])
                .getDisplayOriginatingAddress();
    }
}      

在 AndroidManifest.xml 裡聲明并權重限:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.lmy.SmsListener"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk
        android:minSdkVersion="7" />

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name">
        <activity
            android:name=".SmsListenerActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action
                    android:name="android.intent.action.MAIN" />
                <category
                    android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name="MyBrocast"
            android:enabled="true">
            <intent-filter>
                <action
                    android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>

        </receiver>
        <service android:name="MyService"></service>

    </application>
    <uses-permission
        android:name="android.permission.RECEIVE_SMS" /><!-- 接收短信權限 -->
    <!-- 添加接收系統啟動消息(用于開機啟動)權限 -->
    <uses-permission
        android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>      

這樣就可以獲得接收到的短信了。

3、Android SmsManager 發送短信

SmsManager 可以在背景發送短信,無需使用者操作,開發者就用這個 SmsManager 功能在背景偷偷給SP發短信,導緻使用者話費被扣。必須添加 android.permission.SEND_SMS 權限。

<uses-permission android:name="android.permission.SEND_SMS" />      

如果短信内容過長,可以使用 SmsManager.divideMessage(String text)方法自動拆分成一個ArrayList 數組,再根據數組長度循環發送。

用 sendMultipartTextMessage(String destinationAddress, string scAddress, ArrayList<String> parts, ArrayList<PendingIntent> sentIntents, ArrayList<PendingIntent> deliveryIntents) 方法發送。參數分别為:号碼,短信服務中心号碼(null 即可),短信内容,短信發送結果廣播PendingIntent,短信到達廣播。

下面寫一個 demo 查移動話費餘額,貼上代碼:

package com.dimos.sendmessage;

import java.util.ArrayList;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;

public class SendMessageActivity extends Activity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        SendReceiver receiver=new SendReceiver();
        IntentFilter filter=new IntentFilter();
        filter.addAction(SendReceiver.ACTION);
        registerReceiver(receiver,filter);
        //必須先注冊廣播接收器,否則接收不到發送結果

        SmsManager smsManager = SmsManager.getDefault();
        Intent intent = new Intent();
        intent.setAction(SendReceiver.ACTION);
        ArrayList<String> divideMessage = smsManager.divideMessage("ye");
        PendingIntent sentIntent = PendingIntent.getBroadcast(this, 1, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
        sentIntents.add(sentIntent);

        try {
            smsManager.sendMultipartTextMessage("10086", null,
                    divideMessage, sentIntents, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}      
package com.dimos.sendmessage;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class SendReceiver extends BroadcastReceiver {

    public static final String ACTION = "action.send.sms";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (ACTION.equals(action)) {
            int resultCode = getResultCode();
            if (resultCode == Activity.RESULT_OK) {
                // 發送成功
                System.out.println("發送成功!");
            } else {
                // 發送失敗
                System.out.println("發送失敗!");
            }
        }
    }
}