0

请我需要捕获在 Android 手机中发送的短信并将其记录在某处。到目前为止,我的尝试似乎没有奏效。这是我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.secure.sms"
    android:versionCode="1"
    android:versionName="0.1" >

    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.WRITE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>


    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <activity
            android:name="org.secure.sms.SecureMessagesActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <service android:name=".MyAlarmService" />

        <receiver
            android:name="org.secure.sms.SmsReceiver"
            android:exported="true" >
            <intent-filter android:priority="999" >
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

        <receiver android:name="org.secure.sms.ServiceReceiver"> 
        <intent-filter> 
            <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>                
        </intent-filter> 
        </receiver> 


        <activity
            android:name="org.secure.sms.MyAlarmService"
            android:label="@string/title_activity_my_alarm_service" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="org.secure.sms.SecureMessagesActivity" />
        </activity>



    </application>

</manifest>

这是我的观察者班

package org.secure.sms;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;

public class ServiceObserver extends ContentObserver {
private Context mContext;

//private String contactId = "", contactName = "";
private String smsBodyStr = "", phoneNoStr = "";
private long smsDatTime = System.currentTimeMillis();
static final Uri SMS_STATUS_URI = Uri.parse("content://sms/out");

public ServiceObserver(Handler handler, Context ctx) {
    super(handler);
    mContext = ctx;
}

public boolean deliverSelfNotifications() {
    return true;
}
public void onChange(boolean selfChange) {
    try{
        //Log.e("Info","Notification on SMS observer");
        Cursor sms_sent_cursor = mContext.getContentResolver().query(SMS_STATUS_URI, null, null, null, null);
        if (sms_sent_cursor != null) {
            if (sms_sent_cursor.moveToFirst()) {
                String protocol = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("protocol"));
                Log.e("Info","protocol : " + protocol);
                int type = sms_sent_cursor.getInt(sms_sent_cursor.getColumnIndex("type"));
                    Log.e("Info","SMS Type : " + type);
                    // for actual state type=2
                    if(type == 2){

                        smsBodyStr = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("body")).trim();
                        phoneNoStr = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("address")).trim();
                        smsDatTime = sms_sent_cursor.getLong(sms_sent_cursor.getColumnIndex("date"));

                        Log.e("Info","SMS Content : "+smsBodyStr);
                        Log.e("Info","SMS Phone No : "+phoneNoStr);
                        Log.e("Info","SMS Time : "+smsDatTime);
                    }
                }
            }

        else
            Log.e("Info","Send Cursor is Empty");
    }
    catch(Exception sggh){
        Log.e("Error", "Error on onChange : "+sggh.toString());
    }{
    super.onChange(selfChange);
}


}
}

这是观察者注册

final Uri SMS_STATUS_URI = Uri.parse("content://sms/out");
        ServiceObserver smsSentObserver = new ServiceObserver(new Handler(), this);
        this.getContentResolver().registerContentObserver(SMS_STATUS_URI, true, smsSentObserver);

编辑:该应用程序现在运行良好,但只能在我只访问该应用程序时捕获 SMS,尽管 Android 显示了在后台运行的进程和服务。这是我暂停的代码

我已经注册了一项服务并暂停了此代码,但如果手机仍处于中转状态,它表示一项服务和一个进程正在运行,但除非我访问该应用程序,否则不会捕获短信。

protected void onPause() {
    if(!TrackerService.isRunning){

        serviceIntent = new Intent(MainActivity.this, TrackerService.class);
        startService(serviceIntent);

        mServiceReceiver = new MyReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(TrackerService.mAction);
        registerReceiver(mServiceReceiver, intentFilter);


     }

    super.onPause();
}
4

2 回答 2

0

我有同样的问题,似乎是一个 Android 错误。我创建的解决方案在这里。首先你必须使用:

final Uri SMS_STATUS_URI = Uri.parse("content://sms");

这将接收发送和接收的短信,要知道其中哪些是您必须使用此代码:

注册观察者:

contentResolver = this.getContentResolver();
        contentResolver.registerContentObserver(Uri.parse("content://sms"), true, new mObserver(new Handler()));

观察者本身:

class mObserver extends ContentObserver {

        // Constructor
        public mObserver(Handler handler) {
            super(handler);
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);

            Log.i("SMSTracker","onChange()");

            // Uri to get all the SMS (sent and received)
            Uri uriSMS = Uri.parse("content://sms");
            Cursor cur = contentResolver.query(uriSMS, null, null, null, null);
            cur.moveToNext();

            // Debug
            final String[] names = cur.getColumnNames();
            for(int n = 0; n < cur.getColumnNames().length; ++n){
                Log.i("SMSTracker", "Column[" + n + "] = " + names[n] + " --> " + cur.getString(cur.getColumnIndex(names[n])));
            }      
            Log.i("SMSTracker", "SMS length: " + cur.getString(cur.getColumnIndex("body")).length() );

            final int type = cur.getInt(cur.getColumnIndex("type"));

            // type == 2 means a successfully sent message and type == 1 means a received message 
            if( (type == 2 || type == 1) && (!lastID.contentEquals(cur.getString(cur.getColumnIndex("_id")))) ){
                String protocol = cur.getString(cur.getColumnIndex("protocol"));
                String smsString = cur.getString(cur.getColumnIndex("body"));
                lastID = cur.getString(cur.getColumnIndex("_id"));

                // Sent SMS
                if(protocol == null){

                }
                // Received SMS
                else{

                }
            }
        }
    }

当协议 == null 时,您发送了一条短信,否则您收到了一条短信。(type == 2 || type == 1)是因为 SMS 似乎是分阶段发送的,当 type == 1 或 type == 2 时,SMS 被发送或接收。以上都是我的经验,希望能帮到你!

于 2013-05-24T17:29:50.253 回答
0

尝试使用这个

final Uri SMS_STATUS_URI = Uri.parse("content://sms");
于 2013-05-24T17:24:28.550 回答