发送SMS时,应用程序在getContentResolver()。query(...)上停止

时间:2017-02-26 20:48:35

标签: java android android-studio service contentobserver

美好的一天,我正在编写有关捕获已发送邮件的应用程序。一切正常,每当我尝试发送短信时都会调用ContentObserver,但是在 onChange(boolean selfChange)方法中,应用程序会删除:

  • Cursor cur = getContentResolver()。query(uriSMS,null,null,null,null) .............................. screen http://imgur.com/a/hE94K
mObserver中的TrackerService.java中的

。当我通过Step Over(F8)对其进行重新排序时,在这一行上它打开了一个 Looper.java ,然后单击 http://imgur.com/a/uh4Dl ...如何要解决这个问题吗?我希望你能理解我的问题并抱歉我的英语不好。非常感谢你!

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>

1 个答案:

答案 0 :(得分:1)

确保您正在注册正确的内容路径。例如: -

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

同样在cursor.query上使用相同的路径