无法捕捉到Android上发送的消息 [英] Unable to capture sent messages on Android

查看:184
本文介绍了无法捕捉到Android上发送的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请我需要捕获的正在Android手机发出了短信和地方记录它。到目前为止,我尝试似乎并没有工作。
这里是我的清单

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=org.secure.sms
    安卓版code =1
    机器人:=的versionName0.1>    <采用-SDK安卓的minSdkVersion =8/>    <使用许可权的android:NAME =android.permission.SEND_SMS/>
    <使用许可权的android:NAME =android.permission.WRITE_SMS/>
    <使用许可权的android:NAME =android.permission.READ_SMS/>
    <使用许可权的android:NAME =android.permission.RECEIVE_SMS/>
    <使用许可权的android:NAME =android.permission.INTERNET对/>
    <使用许可权的android:NAME =android.permission.READ_PHONE_STATE/>
    <应用
        机器人:图标=@绘制/图标
        机器人:标签=@字符串/ APP_NAME>
        <活动
            机器人:名字=org.secure.sms.SecureMessagesActivity
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <服务机器人。MyAlarmServiceNAME = />        <接收
            机器人:名字=org.secure.sms.SmsReceiver
            机器人:出口=真正的>
            <意向过滤器的android:优先=999>
                <作用机器人:名字=android.provider.Telephony.SMS_RECEIVED/>
            &所述; /意图滤光器>
        < /接收器>        <接收机器人:名字=org.secure.sms.ServiceReceiver>
        &所述;意图滤光器>
            <作用机器人:名字=android.provider.Telephony.SMS_RECEIVED/>
            <作用机器人:名字=android.net.conn.CONNECTIVITY_CHANGE/>
        &所述; /意图滤光器>
        < /接收器>
        <活动
            机器人:名字=org.secure.sms.MyAlarmService
            机器人:标签=@字符串/ title_activity_my_alarm_service>
            &所述;元数据
                机器人:名字=android.support.PARENT_ACTIVITY
                机器人:值=org.secure.sms.SecureMessagesActivity/>
        < /活性GT;    < /用途>< /清单>

下面是我的观察器类

 包org.secure.sms;
进口的java.util.ArrayList;
进口的java.util.List;进口org.apache.http.HttpEntity;
进口org.apache.http.Htt presponse;
进口org.apache.http.NameValuePair;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.entity.UrlEn codedFormEntity;
进口org.apache.http.client.methods.HttpPost;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.apache.http.message.BasicNameValuePair;
进口org.apache.http.protocol.HTTP;
进口org.apache.http.util.EntityUtils;进口android.content.Context;
进口android.database.ContentObserver;
进口android.database.Cursor;
进口android.net.Uri;
进口android.os.Handler;
进口android.util.Log;公共类ServiceObserver扩展ContentObserver {
私人语境mContext;//私人字符串的ContactID =,联系人姓名=;
私人字符串smsBodyStr =,phoneNoStr =;
私人长期smsDatTime = System.currentTimeMillis的();
静态最终乌里SMS_STATUS_URI = Uri.parse(内容://短信/出);公共ServiceObserver(处理程序处理程序,上下文CTX){
    超(处理);
    mContext = CTX;
}公共布尔deliverSelfNotifications(){
    返回true;
}
公共无效的onChange(布尔selfChange){
    尝试{
        //Log.e(\"Info\",\"Notification短信观察者);
        光标s​​ms_sent_cursor = mContext.getContentResolver()查询(SMS_STATUS_URI,NULL,NULL,NULL,NULL);
        如果(sms_sent_cursor!= NULL){
            如果(sms_sent_cursor.moveToFirst()){
                字符串协议= sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex(协议));
                Log.e(信息,协议:+协议);
                整型= sms_sent_cursor.getInt(sms_sent_cursor.getColumnIndex(类型));
                    Log.e(信息,短信类型:+型);
                    //实际状态类型= 2
                    如果(类型== 2){                        smsBodyStr = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex(身体))修剪()。
                        phoneNoStr = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex(地址))修剪()。
                        smsDatTime = sms_sent_cursor.getLong(sms_sent_cursor.getColumnIndex(DATE));                        Log.e(信息,短信内容:+ smsBodyStr);
                        Log.e(信息,短信电话号码:+ phoneNoStr);
                        Log.e(信息,短信时间:+ smsDatTime);
                    }
                }
            }        其他
            Log.e(信息,发送光标为空);
    }
    赶上(例外sggh){
        Log.e(错误,关于的onChange错误:+ sggh.toString());
    } {
    super.onChange(selfChange);
}
}
}

下面是观察者注册

 最后乌里SMS_STATUS_URI = Uri.parse(内容://短信/出);
        ServiceObserver smsSentObserver =新ServiceObserver(新的处理程序(),这一点);
        。this.getContentResolver()registerContentObserver(SMS_STATUS_URI,真实,smsSentObserver);

编辑:
该应用程序现在工作正常,但我只是能够捕捉到短信的时候,我只访问应用程序,虽然Android的展示流程和服务的研究背景运行。这里是code,我已经把在暂停

我已经注册的服务,并有暂停此code,但如果手机进入iddle,它说一个服务,一个进程在运行,但没有捕捉短信,除非我访问的应用程序。

 保护无效的onPause(){
    如果(!TrackerService.isRunning){        serviceIntent =新意图(MainActivity.this,TrackerService.class);
        startService(serviceIntent);        mServiceReceiver =新MyReceiver();
        IntentFilter的IntentFilter的=新的IntentFilter();
        intentFilter.addAction(TrackerService.mAction);
        registerReceiver(mServiceReceiver,IntentFilter的);
     }    super.onPause();
}


解决方案

我有同样的问题,似乎是一个Android的错误。我创办的解决方案是<一个href=\"http://stackoverflow.com/questions/12683116/why-my-contentobserver-is-not-being-called-when-i-send-an-sms\"标题=点击这里>此处。首先,你必须是:

 最后乌里SMS_STATUS_URI = Uri.parse(内容:// SMS);

这将收到发送和接收的短信,知道其中哪些是你必须使用code:

注册观察者:

  ContentResolver的= this.getContentResolver();
        contentResolver.registerContentObserver(Uri.parse(内容://短信),真,新mObserver(新的处理程序()));

观察员本身:

 类mObserver扩展ContentObserver {        //构造
        公共mObserver(处理程序处理){
            超(处理);
        }        @覆盖
        公共无效的onChange(布尔selfChange){
            super.onChange(selfChange);            Log.i(简讯追踪程式,的onChange());            //乌里让所有的短信(发送和接收)
            乌里uriSMS = Uri.parse(内容:// SMS);
            光标CUR = contentResolver.query(uriSMS,NULL,NULL,NULL,NULL);
            cur.moveToNext();            //调试
            最终的String [] =名称cur.getColumnNames();
            对于(INT N = 0; N&LT; cur.getColumnNames()长; ++ N){
                Log.i(简讯追踪程式,列[+ N +] =+名字[N] + - &gt;中+ cur.getString(cur.getColumnIndex(名称[N])));
            }
            Log.i(简讯追踪程式,手机短信的长度:+ cur.getString(cur.getColumnIndex(身体))长());            最后整型= cur.getInt(cur.getColumnIndex(类型));            //类型== 2意味着成功发送的信息和类型== 1表示收到的消息
            如果((类型== 2 ||类型== 1)及;&放大器;!(lastID.contentEquals(cur.getString(cur.getColumnIndex(_编号))))){
                字符串协议= cur.getString(cur.getColumnIndex(协议));
                字符串smsString = cur.getString(cur.getColumnIndex(身体));
                lastID = cur.getString(cur.getColumnIndex(_ ID));                //短信发送
                如果(协议== NULL){                }
                //收到的短信
                其他{                }
            }
        }
    }

在协议== NULL您发送短信否则你收到之一。 (类型== 2 ||式== 1)是因为短信似乎分阶段发送,当类型== 1型或2 ==短信发送或好评。都是基于我的经验,希望他们能帮助你!

Please I need to capture sms that's being sent out in an Android phone and log it somewhere. So far my attempts don't seem to work. Here is my manifest

<?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>

Here is my observer class

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);
}


}
}

Here is the observer registration

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);

Edit: The app is now working fine but am only able to capture SMS when I only visit the app, though Android showing the process and service running in the backgroud. Here is the code I've put on pause

I've registered a service and have this code on pause, but still if phone goes iddle, it says one service and one process running, but not capturing sms only unless I visit the app.

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();
}

解决方案

I had the same issue and seems an Android bug. The solution I founded is here. First you have to use:

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

This will recieve sent and received SMS, to know which of them is you have to use this code:

Register the observer:

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

The observer itself:

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{

                }
            }
        }
    }

When protocol == null you have sent a SMS else you have received one. (type == 2 || type == 1) is because SMS seems to be sent in stages, when type == 1 or type == 2 the SMS was sent or received. All is based on my experiences hope they can help you!

这篇关于无法捕捉到Android上发送的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆