我是Android的新手,我正在使用android 2.1进行一些sms_receive的事情:当收到短信时,它将无法工作......我有一个力量关闭,帮助!
public class SmsReceiverActivity extends Activity {
private static final String SHORTCODE = "55443";
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object[] messages = (Object[]) bundle.get("pdus");
SmsMessage[] sms = new SmsMessage[messages.length];
//Create messages for each incoming PDU
for (int n = 0; n < messages.length; n++) {
sms[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
for (SmsMessage msg: sms) {
//Verify if the message came from our known sender
if (TextUtils.equals(msg.getOriginatingAddress(), "SHORTCODE")) {
Toast.makeText(context, "Received message from the mothership: " + msg.getMessageBody(),Toast.LENGTH_SHORT).show();
}
}
}
android manifest:
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application android:label="@string/app_name">
<activity
android:name=".SmsReceiver"
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 android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
在安装程序时我想运行它,我有一个强制关闭,这是我的LogCat:
08-26 12:36:58.531: D/AndroidRuntime(700): Shutting down VM
08-26 12:36:58.531: W/dalvikvm(700): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
08-26 12:36:58.573: E/AndroidRuntime(700): Uncaught handler: thread main exiting due to uncaught exception
08-26 12:36:58.621: E/AndroidRuntime(700): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.smsreceiver/com.example.smsreceiver.SmsReceiver}: java.lang.ClassNotFoundException: com.example.smsreceiver.SmsReceiver in loader dalvik.system.PathClassLoader@44c07980
08-26 12:36:58.621: E/AndroidRuntime(700): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
08-26 12:36:58.621: E/AndroidRuntime(700): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
08-26 12:36:58.621: E/AndroidRuntime(700): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
08-26 12:36:58.621: E/AndroidRuntime(700): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
08-26 12:36:58.621: E/AndroidRuntime(700): at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 12:36:58.621: E/AndroidRuntime(700): at android.os.Looper.loop(Looper.java:123)
08-26 12:36:58.621: E/AndroidRuntime(700): at android.app.ActivityThread.main(ActivityThread.java:4363)
08-26 12:36:58.621: E/AndroidRuntime(700): at java.lang.reflect.Method.invokeNative(Native Method)
08-26 12:36:58.621: E/AndroidRuntime(700): at java.lang.reflect.Method.invoke(Method.java:521)
08-26 12:36:58.621: E/AndroidRuntime(700): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-26 12:36:58.621: E/AndroidRuntime(700): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-26 12:36:58.621: E/AndroidRuntime(700): at dalvik.system.NativeStart.main(Native Method)
08-26 12:36:58.621: E/AndroidRuntime(700): Caused by: java.lang.ClassNotFoundException: com.example.smsreceiver.SmsReceiver in loader dalvik.system.PathClassLoader@44c07980
08-26 12:36:58.621: E/AndroidRuntime(700): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
08-26 12:36:58.621: E/AndroidRuntime(700): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
08-26 12:36:58.621: E/AndroidRuntime(700): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
08-26 12:36:58.621: E/AndroidRuntime(700): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-26 12:36:58.621: E/AndroidRuntime(700): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
08-26 12:36:58.621: E/AndroidRuntime(700): ... 11 more
08-26 12:36:58.651: I/dalvikvm(700): threadid=7: reacting to signal 3
08-26 12:36:58.651: E/dalvikvm(700): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
答案 0 :(得分:0)
我认为您需要在 Manifest.xml 中进行一些更改,例如:
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="PACKAGE_NAME.android.action.broadcast"/>
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
将您的班级名称 SmsReceiverActivity 更改为 SmsReceiver ,您的班级也需要扩展 BroadcastReceiver 。
从这里了解如何做到这一点:
答案 1 :(得分:0)
您需要BroadcastReceiver
才能在应用中阅读短信。您可以使用以下内容。
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
Object[] pdus = (Object[]) intent.getExtras().get("pdus");
SmsMessage shortMessage = SmsMessage.createFromPdu((byte[]) pdus[0]);
Log.i("sms received", "sms received");
Log.i("SMS: ", shortMessage.getDisplayMessageBody());
}
}
您还需要通过以下方式将其声明到您的应用程序中。
<receiver android:name="YOUR_PACKAGE_NAME.SmsReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
不要忘记添加与SMS相关的权限,例如以下
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>