2017-02-26 74 views
0

美好的一天,我正在編寫有關捕獲已發送消息的應用程序。一切正常,該ContentObserver被稱爲每次當我嘗試發送短信,但在的onChange(布爾selfChange)方法應用在下降。當發送短信時,應用程序停止在getContentResolver()。查詢(...)

  • 光標CUR = getContentResolver()查詢(uriSMS, NULL,NULL,NULL,NULL) ..............................屏幕http://imgur.com/a/hE94K

在TrackerService .java在mObserver。當我通過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> 
+0

getContentResolver()的查詢(uriSMS,NULL,NULL,NULL,NULL);返回java.lang.SecurityException – fauzt

+0

SMS權限是危險的權限,所以如果您運行在棉花糖或以上,並且具有23+的targetSdkVersion,則需要在運行時請求它們。 http://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it另外,'「content:// sms/out /」'是一個無效的URI。它是「發件箱」,而不是「出」。 –

+0

非常感謝,@MikeM。我將targetSdkVersion更改爲19,然後發送到發件箱並且它正在工作,但是現在我得到此命令的錯誤String smsText = cur.getString(cur.getColumnIndex(「body」));它返回此http:/ /imgur.com/a/hIUK5 – fauzt

回答

0

確保您正在註冊正確的內容路徑。例如: -

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

+0

謝謝你的反應,我修正了它,但現在我得到這個命令的錯誤String smsText = cur.getString(cur.getColumnIndex(「body」)),它返回這個imgur.com/a/hIUK5 ,你有一些提示,它應該是什麼? – fauzt

+0

在訪問遊標之前,您可能需要添加支票。 if(cursor.getCount()> 0 && cursor.moveToFirst){String smsText = cursor.getString(cursor.getColumnIndex(「body」));也嘗試訪問內容://短信/發送,而不是發件箱 – albeee

+0

謝謝好男人,內容://短信/發送/固定它!我很開心:) – fauzt

相關問題