为什么当我发短信我ContentObserver不被叫什么名字?当我、不被、发短信、叫什么名字

2023-09-05 03:23:43 作者:是你又如何

我想检测时,短信发送。我有搜索的网站上和计算器和一切似乎是相同的解决方案。 我有一个简单的活动,启动服务,并在服务中,我试图检测时,短信发送:

I am trying to detect when a SMS is sent. I have searched on the web and on StackOverflow and all seems to be the same solution. I have a simple Activity that starts a Service and in the service I am trying to detect when a SMS is sent:

MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

    Intent serviceIntent;
    private static MyReceiver mServiceReceiver; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    protected void onPause() {
        Log.i("Status","Pause");
        unregisterReceiver(mServiceReceiver);
        super.onPause();
    }

    @Override
    protected void onResume() {
        Log.i("Status","Resume");

        // Inicio el Servicio
        serviceIntent = new Intent(MainActivity.this, TrackerService.class);
        startService(serviceIntent);
        // Registro el broadcast del Service para obtener los datos
        mServiceReceiver = new MyReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(TrackerService.mAction);
        registerReceiver(mServiceReceiver, intentFilter);

        super.onResume();
    }

    /**
     * Receiver del Service, aqui se obtienen los datos que envia el Service
     */
    private class MyReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            Log.i("ServiceReceiver", "onReceive()");
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

TrackerService.java

import android.app.Service;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class TrackerService extends Service{

    // Nombre del service
    public static final String mAction = "SMSTracker";
    ContentResolver content;
    ContentResolver contentResolver;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("Status","Service Start");

        // ContentResolver para obtener los SMS salientes
        contentResolver = this.getContentResolver();
        contentResolver.registerContentObserver(Uri.parse("content://sms/out"), true, new mObserver(new Handler()));

        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * Observer que obtiene los SMS salientes
     */
    class mObserver extends ContentObserver {

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

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            Log.i("Status","onChange");

            Uri uriSMS = Uri.parse("content://sms/out");
            Cursor cur = contentResolver.query(uriSMS, null, null, null, null);
            Log.i("SMS", "Columns: " + cur.getColumnNames());

            cur.moveToNext();
            String smsText = cur.getString(cur.getColumnIndex("body"));

            Log.i("SMS", "SMS Lenght: " + smsText.length());

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("Status","Service Destroy");
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.i("Status","Service Bind");
        return null;
    }

}

清单

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
    <uses-permission android:name="android.permission.READ_SMS"></uses-permission>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

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

</application>

据我LogCat中的服务已启动,我可以看到它在我的Andr​​oid手机的onChange()在我的服务方法运行的服务不会被调用时,我发短信。是我丢失的东西?

According to my LogCat the service is started and I can see it in running services in my Android phone onChange() method inside my service is never called when I send an sms. Is something I am missing ?

推荐答案

好了,好像也没办法,鸵鸟政策知道这是我的错误在Android的错误,但我得到了它的工作:

Ok, seems there is no way, don´t know if it is my error on an Android bug but I got it working:

        if( (type == 2 || type == 1) && (!lastID.contentEquals(cur.getString(cur.getColumnIndex("_id")))) ){
            String protocol = cur.getString(cur.getColumnIndex("protocol"));
            lastID = cur.getString(cur.getColumnIndex("_id"));
            // Mensaje enviado
            if(protocol == null){
                Log.i("SMSStatus", "SMS Sent");
            }
            // Mensaje recibido
            else{
                Log.i("SMSStatus", "SMS received");
            }
        }

当收到短信是TYPE = 1,当短信发送它的类型是6,4,当它被送到最后是类型= 2。要当心,因为有时的onChange方法被调用很多次没有问题如果只有一个短信,所以你应该检测,如果是同样的短信通过检查_id参数。

When an SMS is received it is type = 1, when an SMS is sent it is type 6, 4 and finally when it is sent is type = 2. Be carefull because sometimes the onChange method is called lots of times no matters if it is only one SMS so you should detect if it is the same SMS by checking the _id parameter.