小弟初学android
写了一个类用来接收模拟器发送的短信:
public class SMSReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object messages[] = (Object[])bundle.get("pdus");
SmsMessage[] smsMessage = new SmsMessage[messages.length];
for(int i=0;i<messages.length;i++){
smsMessage[i] = SmsMessage.createFromPdu((byte[])messages[i]);
}
Toast toast = Toast.makeText(context, smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
toast.show();
}
}
AndroidManifest.xml 配置文件内容
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HelloWorld"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SMSReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
模拟接收短信时
LogCat后台抛出异常:
could't open fd for content://setting//system/notification_sound
详解见附件,请各位帮忙看一下。
问题补充
没人知道吗?
[quote]与HelloWorld无关。主要是用BroadcastReceiver类。
就是照着《Android应用开发揭秘》4.2.3做一个小的例子。。[/quote]
主要是你的是你配置的问题,你的AndroidManifest.xml配置文件中没有配置user-permission,也就是样
[code="java"]
android:label="@string/app_name">
<receiver android:name=".SMSReceiver" android:enabled="true">
[/code]
这样的话你通过模拟器发就好了。就能在HelloWorld中显示Message内容了 :D
能看看你.HelloWorld这个类里面的代码吗??可能是配置有问题 :o
[quote]
谢谢,这是HelloWorld类。
package com.android;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class HelloWorld extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.toast);
Button centerBn = (Button)findViewById(R.id.centerBn);
centerBn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
displayToast("接收到短信,将会显示");
}
});
}
public void displayToast(String msg){
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
}
[/quote]
我感觉你理解可能有错误。先说说你想要实现的什么吧。你是想在HelloWorld中接收外面发来的信息对么?我理解的正确吗? :o
[quote]我感觉你理解可能有错误。先说说你想要实现的什么吧。你是想在HelloWorld中接收外面发来的信息对么?我理解的正确吗?[/quote]
还是你想通过模拟器发送消息给HelloWorld啊?