Android 读取、接收、发送 手机短信

https://www.cnblogs.com/ycclmy/tag/android/

1、Android 读取手机短信

From:https://www.cnblogs.com/ycclmy/p/3193075.html

获取 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("发送失败!");
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/freeking101/article/details/121575985
今日推荐