0

私は次のコードを持っています:

public class SmsReceiver extends BroadcastReceiver
{
    private DBOperations DataDBoperation;
    String str = ""; 
    String password;
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;

        if (bundle != null)
        {
            //---retrieve the SMS message received---
            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]);                             
                str += msgs[i].getMessageBody().toString();
            }
            //---if incomming msg is equal to Msg pattern
            int triggerCommand = getMsgPattern(context);
            if(triggerCommand == 1 || triggerCommand == 2 || triggerCommand == 3 || triggerCommand == 4 || triggerCommand == 5 || triggerCommand == 6)
            {
                switch(triggerCommand)
                {
                case 1:
                {   ///// BACKUP
                    Intent i=new Intent(context,UploadContent.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
                    context.startActivity(i);
                    break;
                }
                case 2:
                {
                    //// FORMAT
                    Intent j=new Intent(context,DeleteContent.class);
                    j.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
                    context.startActivity(j);
                    break;
                }
                }
                //-------- delete this sms here ----------
                abortBroadcast();
            }
        } 

public int getMsgPattern(Context c)
    {
        DataDBoperation = new DBOperations(c);
        String[] valuesPassword = DataDBoperation.getUserPasswordsString();
        password = valuesPassword[0];
        String pattern1 = "BACKUP *" + password + "*";
        String pattern2 = "FORMAT *" + password + "*";
        String pattern3 = "LOCATE *" + password + "*";
        String pattern4 = "WIPE *" + password + "*";
        String pattern5 = "LOCK *" + password + "*";
        String pattern6 = "RESETPASSWORD *" + password + "*";

        if(pattern1.equalsIgnoreCase(str))
        { 
            return 1;
        }
        else if(pattern2.equalsIgnoreCase(str))
        {
            return 2;
        }
return 0;
    }                        
    }

エラーログ:

10-11 12:59:54.842: E/AndroidRuntime(18743): FATAL EXCEPTION: main
10-11 12:59:54.842: E/AndroidRuntime(18743): java.lang.RuntimeException: Unable to start receiver iiu.fyp.cdsr.SmsReceiver: java.lang.NullPointerException
10-11 12:59:54.842: E/AndroidRuntime(18743):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2379)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at android.app.ActivityThread.access$1500(ActivityThread.java:149)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at android.os.Looper.loop(Looper.java:153)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at android.app.ActivityThread.main(ActivityThread.java:5026)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at java.lang.reflect.Method.invokeNative(Native Method)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at java.lang.reflect.Method.invoke(Method.java:511)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at dalvik.system.NativeStart.main(Native Method)
10-11 12:59:54.842: E/AndroidRuntime(18743): Caused by: java.lang.NullPointerException
10-11 12:59:54.842: E/AndroidRuntime(18743):    at iiu.fyp.cdsr.DBOperations.getUserPasswordsString(DBOperations.java:149)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at iiu.fyp.cdsr.SmsReceiver.getMsgPattern(SmsReceiver.java:89)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at iiu.fyp.cdsr.SmsReceiver.onReceive(SmsReceiver.java:31)
10-11 12:59:54.842: E/AndroidRuntime(18743):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2372)
10-11 12:59:54.842: E/AndroidRuntime(18743):    ... 10 more

特定のSMSを受信したときにアクティビティを呼び出し、単語の後に受信ボックスから削除する必要があります.abortbroadcast()のために、SMSサービスよりも優先度を高く設定しました..実際の問題はどこですか?誰かがそれを修正できますか?

編集:

以下は、sqlite からパスワードを取得するための私の DB コードです。

    public String[] getUserPasswordsString() {
                List PasswordClass = new ArrayList();

                Cursor cursor = database.query(DataBaseWrapper.PasswordTable,
                        PASSWORD_TABLE_COLUMNS, null, null, null, null, null);

                cursor.moveToFirst();
                String[ ] password = new String[1] ;
                int i=0;
                while (!cursor.isAfterLast()) {
                    PasswordClass sim = parsePasswords(cursor);
                    password[i] = sim.getUserPassword().toString();
                    i++;
                    PasswordClass.add(sim);
                    cursor.moveToNext();
                }


                cursor.close();
                return password;
            }
private PasswordClass parsePasswords(Cursor cursor) {
            PasswordClass UserPassword = new PasswordClass();
            UserPassword.setUserPasswordId((cursor.getInt(0)));
            UserPassword.setUserPassword(cursor.getString(1));
            return UserPassword;
        }
4

2 に答える 2

0

マニフェストに許可を追加しましたか。追加する必要があります

<uses-permission android:name="android.permission.RECEIVE_SMS"> 

さらに問題がある場合は、以下のリンクに従ってください

AndroidでSMSを受信する方法

于 2013-10-11T09:40:50.407 に答える