我正在创建一个应用程序,只需要从所有数字中收听包含文本“FIND”的SMS消息。我已经编写了代码,但无论何时都崩溃了 收到短信。请帮忙!
public class SMSReceiver extends BroadcastReceiver {
Context Context;
@Override
public void onReceive(Context context, Intent intent) {
this.Context = context;
if (intent.getAction()
.equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if (bundle != null) {
try {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
String msgBody = msgs[i].getMessageBody();
if (msgBody == "FIND") {
Toast toast = Toast.makeText(context,
"SMS RECEIVED", Toast.LENGTH_LONG);
toast.show();
}
}
} catch (Exception e) {
// Log.d("Exception caught",e.getMessage());
}
}
}
}
}
这是Logcat:
02-18 13:01:23.547: W/dalvikvm(1264): threadid=1: thread exiting with uncaught exception (group=0xb5f624f0)
02-18 13:01:23.547: E/AndroidRuntime(1264): FATAL EXCEPTION: main
02-18 13:01:23.547: E/AndroidRuntime(1264): java.lang.RuntimeException: Unable to instantiate receiver pv.ilostmyphone.listener.SmsListener: java.lang.ClassNotFoundException: pv.ilostmyphone.listener.SmsListener in loader dalvik.system.PathClassLoader[/data/app/pv.ilostmyphone-1.apk]
02-18 13:01:23.547: E/AndroidRuntime(1264): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1773)
02-18 13:01:23.547: E/AndroidRuntime(1264): at android.app.ActivityThread.access$2400(ActivityThread.java:117)
02-18 13:01:23.547: E/AndroidRuntime(1264): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
02-18 13:01:23.547: E/AndroidRuntime(1264): at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 13:01:23.547: E/AndroidRuntime(1264): at android.os.Looper.loop(Looper.java:130)
02-18 13:01:23.547: E/AndroidRuntime(1264): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-18 13:01:23.547: E/AndroidRuntime(1264): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 13:01:23.547: E/AndroidRuntime(1264): at java.lang.reflect.Method.invoke(Method.java:507)
02-18 13:01:23.547: E/AndroidRuntime(1264): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-18 13:01:23.547: E/AndroidRuntime(1264): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-18 13:01:23.547: E/AndroidRuntime(1264): at dalvik.system.NativeStart.main(Native Method)
02-18 13:01:23.547: E/AndroidRuntime(1264): Caused by: java.lang.ClassNotFoundException: pv.ilostmyphone.listener.SmsListener in loader dalvik.system.PathClassLoader[/data/app/pv.ilostmyphone-1.apk]
02-18 13:01:23.547: E/AndroidRuntime(1264): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
02-18 13:01:23.547: E/AndroidRuntime(1264): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
02-18 13:01:23.547: E/AndroidRuntime(1264): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-18 13:01:23.547: E/AndroidRuntime(1264): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1764)
02-18 13:01:23.547: E/AndroidRuntime(1264): ... 10 more
02-18 13:02:13.797: E/AndroidRuntime(1306): ... 10 more
这是我的Android清单 -
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pv.ilostmyphone"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".SMSReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" >
</action>
</intent-filter>
</receiver>
</application>
</manifest>
答案 0 :(得分:0)
这是来自类似应用的代码
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdusObj = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdusObj.length];
// getting SMS information from Pdu.
for (int i = 0; i < pdusObj.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
}
for (SmsMessage currentMessage : messages) {
String sender = currentMessage.getDisplayOriginatingAddress();
String message = currentMessage.getDisplayMessageBody();
messageReceived(sender, message);
}
}
}
protected void messageReceived(String sender, String message){
if(message.equals("FIND"){
//DO whatever here
}
}
}
答案 1 :(得分:0)
你正在以错误的方式比较String,在Java中.equals()方法用于比较两个字符串,所以改变
if (msgBody == "FIND")
到
if (msgBody.equals("FIND") )
但为了更好的结果,我建议你尝试使用startsWith()方法,
if (msgBody.startsWith("FIND") )
以上这一行的原因是我确信你的msg包含更多单词。
答案 2 :(得分:0)
我通过重新制作我的项目解决了这个问题。我创建了一个MainActivity.java,然后在src下创建了另一个以这种格式命名的包“(包名).Broadcast”。我在这个包中创建了SMSReceiver.java。然后,我将清单中的接收器更改为:
<receiver android:name=".Broadcast.SMSReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" >
</action>
</intent-filter>
</receiver>