我在SO上看到@Commonsware声明接收SMS所需的全部是广播接收器(即没有使用服务)。我想采用这种方法,因为它对我有意义。不幸的是,似乎没有在我的广播接收器中接收/处理SMS。 apk已成功安装,但在发送测试SMS时没有生成日志条目。我究竟做错了什么?
这是清单:
<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 Receiver代码。它的目的是通过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 :(得分:2)
代码在我看来是正确的。 您可以尝试通过max ..
增加优先级 <intent-filter android:priority="2147483647">
可能有这样的情况,当其他接收者在您之前获取短信并中止它们时。
答案 1 :(得分: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,只需读取高数字等于更高优先级。
答案 2 :(得分:0)
删除android:priority
属性
答案 3 :(得分:0)
基于android开发者doc必须是-1000&gt; priority&lt; 1000因此intent-filter的最高优先级是999