2013-01-18 50 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