代码之家  ›  专栏  ›  技术社区  ›  GG.

使用广播接收器接收短信息时出错

  •  0
  • GG.  · 技术社区  · 15 年前

    我试着用广播接收器接收短信。mysm接收器:

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.telephony.SmsMessage;
    import android.util.Log;
    
    public class MySmsReceiver extends BroadcastReceiver {
        /** Tag string for our debug logs */
        private static final String TAG = "MySmsReceiver";
    
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i(TAG, "Recieved a message");
            Bundle extras = intent.getExtras();
            if (extras == null)
                return;
    
            Object[] pdus = (Object[]) extras.get("pdus");
    
            for (int i = 0; i < pdus.length; i++) {
                SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
                String fromAddress = message.getOriginatingAddress();
                String fromDisplayName = fromAddress;
    
                Log.i(TAG, fromAddress);
                Log.i(TAG, fromDisplayName);
                Log.i(TAG, message.getMessageBody().toString());
    
                break;          
            }
        }
    }
    

    并添加到Mainfest文件中

    <receiver android:name=".MySmsReceiver" android:enabled="false">
                <intent-filter>
                    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                </intent-filter>
            </receiver>
        <uses-permission android:name="android.permission.RECEIVE_SMS" />
    

    但当我运行这个应用程序并使用模拟器发送短信时,它会在logcat中显示以下日志

    10-12 00:14:53.082: VERBOSE/Telephony(1032): getOrCreateThreadId uri: content://mms-sms/threadID?recipient=9898989898
    10-12 00:14:53.203: VERBOSE/Telephony(1032): getOrCreateThreadId cursor cnt: 1
    10-12 00:14:53.432: DEBUG/Mms:app(1032): getSmsNewMessageNotificationInfo: count=4, first addr=9898989898, thread_id=3
    10-12 00:14:53.482: WARN/NotificationService(62): STOP command without a player
    10-12 00:14:53.562: DEBUG/MediaPlayer(62): Couldn't open file on client side, trying server side
    10-12 00:14:53.582: ERROR/MediaPlayerService(34): Couldn't open fd for content://settings/system/notification_sound
    10-12 00:14:53.592: ERROR/MediaPlayer(62): Unable to to create media player
    10-12 00:14:53.643: WARN/NotificationService(62): error loading sound for content://settings/system/notification_sound
    10-12 00:14:53.643: WARN/NotificationService(62): java.io.IOException: setDataSource failed.: status=0x80000000
    10-12 00:14:53.643: WARN/NotificationService(62):     at android.media.MediaPlayer.setDataSource(Native Method)
    10-12 00:14:53.643: WARN/NotificationService(62):     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:716)
    10-12 00:14:53.643: WARN/NotificationService(62):     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:671)
    10-12 00:14:53.643: WARN/NotificationService(62):     at com.android.server.NotificationPlayer$CreationAndCompletionThread.run(NotificationPlayer.java:88)
    
    

    告诉我原因是什么。

    1 回复  |  直到 12 年前
        1
  •  2
  •   GG.    15 年前