-1

我是 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();
            }
        }
}

安卓清单:

<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
4

2 回答 2

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>

不要忘记添加与短信相关的权限,例如以下

<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
于 2013-08-28T07:42:09.563 回答
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
从这里查看如何操作:

于 2013-08-28T07:40:32.390 回答