收到短信后启动活动

时间:2016-02-19 14:39:08

标签: android android-studio sms android-broadcastreceiver

所以我试图在条件之后找到一种方法来开始新的活动。尝试调用函数但它不起作用。我希望有人能帮帮忙。我只是一个初学者,请尊重。

if (bundle != null) {
            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]);
                messageReceived += msgs[i].getMessageBody().toString();
                messageReceived += "\n";

            }

            senderPhoneNumber = msgs[0].getOriginatingAddress();


        }

        if ((messageReceived == "xxxxxx") && (senderPhoneNumber == "xxxxxxxxx")) {

           //If these conditions are true I want to start a new Activity
        }

1 个答案:

答案 0 :(得分:0)

您可以使用Activity方法的上下文参数启动onRecieve()

 @Override
    public void onReceive(Context context, Intent intent) {
        //start activity
        Intent i = new Intent();
        i.setClassName("com.example", "com.example.ActivityName");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }