0

Good day, I am programming application about catching the sent messages. Everything is working, the ContentObserver is called everytime when I try to send a SMS, but in onChange(boolean selfChange) method the application drops on the :

in TrackerService.java in mObserver. When I am debbuging that via Step Over (F8), on this line It open me a Looper.java and drops on this lines https://i.stack.imgur.com/YPbxe.jpg ... How to fix that for working please? I hope you will understand my problem and sorry for my bad english. Thank you so much!

MainActivity.java

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
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.activity_main);
    }

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

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

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

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

        super.onResume();
    }

    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
{
    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 = this.getContentResolver();
        contentResolver.registerContentObserver(Uri.parse("content://sms/"), true, new mObserver(new Handler()));

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

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

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="wut.com.smstry">

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

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

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

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

    </application>

</manifest>
fauzt
  • 71
  • 10
  • getContentResolver().query(uriSMS, null, null, null, null); returns java.lang.SecurityException – fauzt Feb 26 '17 at 21:08
  • The SMS permissions are dangerous permissions, so you need to request them at runtime if you're running on Marshmallow or above, with a `targetSdkVersion` of 23+. http://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it Also, `"content://sms/out/"` is an invalid URI. It's `outbox`, not `out`. – Mike M. Feb 26 '17 at 22:17
  • Thank you so much, @MikeM. I changed a targetSdkVersion to 19 and out on outbox and it is working, but now I am getting an error on the this command String smsText = cur.getString(cur.getColumnIndex("body"));, It returns this http://imgur.com/a/hIUK5 – fauzt Feb 26 '17 at 22:45
  • You need to check what `moveToNext()` returns. If it returns false there, then the returned `Cursor` is empty. I don't think `outbox` is what you're thinking it is. Messages don't stay there very long, if they send successfully. Maybe you want `sent` instead? – Mike M. Feb 26 '17 at 22:55
  • Yes, the sent is working for me :) Thank you for your time ! – fauzt Feb 26 '17 at 23:10

1 Answers1

2

Make sure you're registering for the right content path. Ex:-

Inbox = "content://sms/inbox" 
Failed = "content://sms/failed" 
Queued = "content://sms/queued" 
Sent = "content://sms/sent" 
Draft = "content://sms/draft" 
Outbox = "content://sms/outbox" 
Undelivered = "content://sms/undelivered" 
All = "content://sms/all" 
Conversations = "content://sms/conversations".


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

Similarly use the same path on the cursor.query

albeee
  • 1,452
  • 1
  • 12
  • 20
  • Thank you for your reaction, I fixed it, but now I am getting an error on the this command String smsText = cur.getString(cur.getColumnIndex("body"));, It returns this imgur.com/a/hIUK5 , do you have some tips, what it should be? – fauzt Feb 26 '17 at 22:49
  • You may have to add a check before you access cursor. if(cursor.getCount() > 0 && cursor.moveToFirst){ String smsText =cursor.getString(cursor.getColumnIndex("body")); also try accessing content://sms/sent instead of outbox – albeee Feb 26 '17 at 22:59
  • Thank you good man, the content://sms/sent/ fixed It !! I am so happy :) – fauzt Feb 26 '17 at 23:06