0

私は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レシーバーコードです。これは、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]
4

4 に答える 4

3

コードは正しいようです。優先度を最大で上げることで試すことができます。

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

他の受信者があなたの前にSMSを取得し、それらを中止している場合があります。

于 2013-01-18T09:45:02.127 に答える
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ではなく高い数値に設定します。高い数値が高い優先度に等しいことを確認してください。

于 2013-01-18T09:43:30.040 に答える
0

android:priority属性を削除します

于 2013-01-18T09:48:43.470 に答える
0

android開発者のドキュメントに基づくと、-1000> priority <1000である必要があるため、intent-filterの最高の優先度は999です。

于 2013-12-09T17:02:15.313 に答える