我已經看到過,@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]
所以你的意思是你沒有得到接收到的SMS的廣播或者你沒有收到短信本身。? –
我沒有收到廣播。我正在收到短信。順便說一句,SMSReceiver是在活動在屏幕上時從其正在工作的活動中獲取的。問題是當活動不在屏幕上時,SMSReceiver不再接收廣播。這就是爲什麼我試圖用這種新方法將接收器與活動分開。有更好的方法去嗎? – PeteH
@AndersMetnik:好主意。我剛剛回顧了答案並接受了答案。我希望我能接受更多! – PeteH