我有一个应用程序可以检查最后一个已知位置,如果它高于预定义的阈值,它应该禁止收到短信的通知,并向发件人发送自动回复。
当我在Emulators之间进行测试时,它会失败并显示以下日志:
11-28 09:07:03.747: E/AndroidRuntime(324): FATAL EXCEPTION: main
11-28 09:07:03.747: E/AndroidRuntime(324): java.lang.RuntimeException: Unable to instantiate receiver biz.midl.drivereply.SMSReceiver: java.lang.ClassNotFoundException: biz.midl.drivereply.SMSReceiver in loader dalvik.system.PathClassLoader[/data/app/biz.midl.drivereply-1.apk]
11-28 09:07:03.747: E/AndroidRuntime(324): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2789)
11-28 09:07:03.747: E/AndroidRuntime(324): at android.app.ActivityThread.access$3200(ActivityThread.java:125)
11-28 09:07:03.747: E/AndroidRuntime(324): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
11-28 09:07:03.747: E/AndroidRuntime(324): at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 09:07:03.747: E/AndroidRuntime(324): at android.os.Looper.loop(Looper.java:123)
11-28 09:07:03.747: E/AndroidRuntime(324): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-28 09:07:03.747: E/AndroidRuntime(324): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 09:07:03.747: E/AndroidRuntime(324): at java.lang.reflect.Method.invoke(Method.java:521)
11-28 09:07:03.747: E/AndroidRuntime(324): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-28 09:07:03.747: E/AndroidRuntime(324): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-28 09:07:03.747: E/AndroidRuntime(324): at dalvik.system.NativeStart.main(Native Method)
11-28 09:07:03.747: E/AndroidRuntime(324): Caused by: java.lang.ClassNotFoundException: biz.midl.drivereply.SMSReceiver in loader dalvik.system.PathClassLoader[/data/app/biz.midl.drivereply-1.apk]
11-28 09:07:03.747: E/AndroidRuntime(324): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
11-28 09:07:03.747: E/AndroidRuntime(324): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
11-28 09:07:03.747: E/AndroidRuntime(324): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
11-28 09:07:03.747: E/AndroidRuntime(324): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2780)
11-28 09:07:03.747: E/AndroidRuntime(324): ... 10 more
这是我的接收器活动:
package biz.midl.drivereply;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
public class PassiveSms extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences sharedPref= context.getSharedPreferences("chaosdriver", Context.MODE_PRIVATE);
int speedLimit = sharedPref.getInt("speedLimit", 5);
String message = sharedPref.getString("message", "I'm sorry, but I am driving. I will text you when I am able!");
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
float currentSpeedms = lastKnownLocation.getSpeed();
float currentSpeedMPH = (float) (currentSpeedms * 2.237);
int newSpeed = (int) currentSpeedMPH;
if (newSpeed > speedLimit)
{
Bundle extras = intent.getExtras();
if (extras == null)
{
return;
}
abortBroadcast();
Object[] pdus = (Object[]) extras.get("pdus");
SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdus[0]);
String origNumber = msg.getOriginatingAddress();
onSend(origNumber, message);
}
}
public void onSend(String px, String message)
{
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(px, null, message, null, null);
}
}
此外,这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="biz.midl.drivereply"
android:versionCode="1"
android:versionName="0.5" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name="biz.midl.drivereply.PassiveLocationChangedReceiver"
android:enabled="true" />
<activity
android:name="biz.midl.drivereply.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="biz.midl.drivereply.Settings"
android:label="@string/title_activity_settings" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="biz.midl.drivereply.PassiveSms"
android:label="@string/title_activity_passive_sms" >
</activity>
<receiver android:name="biz.midl.drivereply.SMSReceiver" >
<intent-filter android:priority="999" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<service android:name="biz.midl.drivereply.MyService" >
<intent-filter>
<action android:name="com.example.MyService" />
</intent-filter>
</service>
<activity
android:name="biz.midl.drivereply.PassiveSMSSend"
android:label="@string/title_activity_passive_smssend" >
</activity>
</application>
</manifest>
我运行了调试,但它没有显示我的接收器步骤/断点。
有关可能导致此问题的任何建议吗?
答案 0 :(得分:0)
我明白了。在我的清单中,我有一些大写问题。这解决了它。