我在名为 SmsRceiver 的 BroadCastReceiver 中接收来自模拟器的短信。现在我想在 Activity 内的文本视图上显示此消息。打开一个活动,我正在从我的 SmsReciever 发送另一个广播。但我收到以下错误
getOrCreateThreadId cursor cnt: 1
01-31 03:28:04.562: W/NotificationService(68): STOP command without a player
01-31 03:28:04.652: D/MediaPlayer(68): Couldn't open file on client side, trying server side
01-31 03:28:04.652: E/MediaPlayerService(33): Couldn't open fd for content://settings/system/notification_sound
01-31 03:28:04.662: E/MediaPlayer(68): Unable to to create media player
01-31 03:28:04.662: W/NotificationService(68): error loading sound for content://settings/system/notification_sound
01-31 03:28:04.662: W/NotificationService(68): java.io.IOException: setDataSource failed.: status=0x80000000
01-31 03:28:04.662: W/NotificationService(68): at android.media.MediaPlayer.setDataSource(Native Method)
01-31 03:28:04.662: W/NotificationService(68): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:737)
01-31 03:28:04.662: W/NotificationService(68): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:692)
01-31 03:28:04.662: W/NotificationService(68): at com.android.server.NotificationPlayer$CreationAndCompletionThread.run(NotificationPlayer.java:88)
短信接收器.Java
package com.blogspot.uappmarket.smschat;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver {
String from,message = "";
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] sms = new SmsMessage[pdus.length];
from += SmsMessage.createFromPdu((byte[]) pdus[0]).getOriginatingAddress();
for (int i=0; i<sms.length; i++){
sms[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
message+=sms[i].getMessageBody();
}
Intent broadcastIntent = new Intent();
intent.setAction("SMS_RECEIVED_ACTION");
intent.putExtra("from", from);
intent.putExtra("message", message);
context.sendBroadcast(broadcastIntent);
}
}
接收短信.Java
package com.blogspot.uappmarket.smschat;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;
public class ReceiveSms extends Activity{
TextView tvFrom,tvMessage;
BroadcastReceiver MessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//tvFrom.setText("One Message Received");
}
};
IntentFilter filter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.receive_sms);
tvFrom = (TextView) findViewById(R.id.tvFrom);
tvMessage = (TextView) findViewById(R.id.textView2);
filter= new IntentFilter("SMS_RECEIVED_ACTION");
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(MessageReceiver, filter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(MessageReceiver);
}
}