2013-01-18 49 views
0

我已經看到過,@Commonsware表示,接收短信所需要的只是一個廣播接收器(即,沒有使用服務)。我想採用這種方法,因爲它對我來說很有意義。不幸的是,它似乎並沒有在我的廣播接收器中收到/處理SMS。 apk安裝成功,但發送測試短信時沒有生成日誌條目。我究竟做錯了什麼?廣播接收器不處理短信的

這裏的清單:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="net.oheller.pete2" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="10" 
     android:targetSdkVersion="17" /> 

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

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/icon_broadcastreceiver" 
     android:label="@string/sms_broadcastreceiver" 
     android:theme="@style/Theme.eagleeyeactionbar" > 

     <receiver android:name=".SMSReceiver_Test" > 
      <intent-filter android:priority="999"> 
       <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
      </intent-filter> 
     </receiver> 

     <provider 
      android:name=".contentprovider.GPSTrackerContentProvider" 
      android:authorities="net.oheller.pete2.contentprovider" 
      android:grantUriPermissions="true" 
      android:multiprocess="true" 
      android:permission="true" > 
     </provider> 
    </application> 

</manifest> 

這裏的短信接收器代碼。它的目的是通過SMS收集數據並將每條數據記錄到內容提供者(SQLite數據庫)。

public class SMSReceiver_Test extends BroadcastReceiver { 
TelephonyManager telephonyManager; 
String deviceCountryCode = ""; 

@Override 
public void onReceive(Context context, Intent intent) { 
    //Get the SMS message passed in from the intent 
    Bundle SMSmessageContent = intent.getExtras(); 
    SmsMessage[] receivedSMS = null; 
    String returnStr = ""; 
    Long current_time_stamp = Calendar.getInstance().getTimeInMillis(); 
    String curTimeString = getFormattedDate(current_time_stamp, "dd-MMM-yy h:mma");  

    telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);  

    String incoming_SMSphone_number = ""; 
    String SMSmsgText = ""; 
    long timeStamp = current_time_stamp; 

    if (SMSmessageContent != null) { // [IF valid SMS] 
     //Retrieve the SMS message info 
     Object[] pdus = (Object[]) SMSmessageContent.get("pdus"); 
     receivedSMS = new SmsMessage[pdus.length]; 
     for (int i=0; i<receivedSMS.length; i++) { // [FOR get SMS content] 
      receivedSMS[i] = SmsMessage.createFromPdu((byte[])pdus[i]); 
      incoming_SMSphone_number = receivedSMS[i].getOriginatingAddress().toString().trim(); 
      SMSmsgText = receivedSMS[i].getMessageBody().toString(); 
      returnStr += curTimeString +"\n"; 

Log.d("EagleEye", String.format("SMSReciever: SMS=\"%s\"", SMSmsgText)) ; ///DEBUG 
     } // [ENDFOR get SMS content] 

      /////////////////////////// 
      // prepare values for insertion into the SQLite DB 
       ContentValues GPSTrackValues = new ContentValues(); 
       GPSTrackValues.put(GPSTrackerTable.COLUMN_PHONE_NUMBER, incoming_SMSphone_number); 
       GPSTrackValues.put(GPSTrackerTable.COLUMN_TIME_STAMP, timeStamp); 

//// Prepare access to content provider 
       Uri GPSuri = GPSTrackerContentProvider.CONTENT_URI_GPS_Tracks; //specify the content provider location 
       ContentResolver GPSTrackCR = context.getContentResolver();   

       // Insert new entry into DB 
       Uri insertURI = GPSTrackCR.insert(GPSuri, GPSTrackValues);  
Log.d("EagleEye", "DB insert results="+insertURI) ; ////////DEBUG 

    } // [ENDIF Valid SMS] 
} // [END onReceive] 
    } // [END SMSReceiver] 
+0

所以你的意思是你沒有得到接收到的SMS的廣播或者你沒有收到短信本身。? –

+0

我沒有收到廣播。我正在收到短信。順便說一句,SMSReceiver是在活動在屏幕上時從其正在工作的活動中獲取的。問題是當活動不在屏幕上時,SMSReceiver不再接收廣播。這就是爲什麼我試圖用這種新方法將接收器與活動分開。有更好的方法去嗎? – PeteH

+0

@AndersMetnik:好主意。我剛剛回顧了答案並接受了答案。我希望我能接受更多! – PeteH

回答

2

代碼在我看來是正確的。 您可以通過增加最高優先嚐試..

<intent-filter android:priority="2147483647"> 

有可能的情況下,當其他接收器是你之前得到短信和中止它們。

+0

尤里卡!設置android:priority =「2147483647」做到了。謝謝。 (順便說一句,你在哪裏找到這個令人印象深刻的號碼?) – PeteH

+0

是的......它很簡單..數字越大,優先級越高。優先級已增加到2^31 - 1,因爲壓縮的xml格式將數字表示爲4字節有符號整數。 – Jambaaz

+0

我無法準確理解,但它確實沒有必要在應用程序中啓動啓動器。甚至可能只有接收器。所以..我懷疑這是一些不同的問題...! – Jambaaz

1

您優先可能不夠好,嘗試將其設置到2147483647

<receiver android:name=".SMSReceiver_Test" > 
     <intent-filter android:priority="2147483647"> 
      <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
     </intent-filter> 
    </receiver> 

這樣做後,嘗試在調試模式下啓動它,在你的onReceive方法斷點,或者做一個Log.i("BroadcastSMSReceiver", "Received something in SMSRECEIVERTEST") :)

編輯,將優先級設置爲高數而不是1,只是讀取高數等於更高優先級。

0

取出android:priority屬性

+0

做一些解釋,但更多地解釋爲什麼? – mtk

+0

@mtk:請參閱我上面的評論。這是否回答你的問題? – PeteH

+0

開發人員協會說:「只有在您確實需要強制收到廣播的特定順序,或者希望強制Android將某項活動優先於其他活動時才使用此屬性。」 我正在玩ANDROID SMS API,並開發了一個測試項目,接收短信和我的清單代碼塊只是 <意圖濾波器> <操作機器人:名稱= 「android.provider.Telephony.SMS_RECEIVED」/> 和做了這項工作。 – IronBlossom

0

基於Android開發者文檔必須-1000>優先< 1000意圖過濾器的如此最高優先級是999