1

如果收到特定的短信并且它具有特定的价值,我该如何开始新的活动?我使用了意图,但在代码中显示错误此错误显示在意图中

未为 SmsReceiver 类型定义方法 startActivity(Intent)

下面是我的代码:

package com.mypackage;

import java.util.Map;

import android.content.BroadcastReceiver;
import android.content.Context;
 import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
 import android.widget.Toast;

  public class SmsReceiver extends BroadcastReceiver {

   public void onReceive(Context context, Intent intent) 
{
    //---get the SMS message passed in---
    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;
    String str = "";            
    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 += "SMS from " + msgs[i].getOriginatingAddress();                     
            str += " :";
            str += msgs[i].getMessageBody().toString();
            str += "\n";     
            if(str.contains("00"));
            {
                Intent k = new Intent(this.getApplicationContext(),    
   AgAppMenu.class);
                startActivity(k);
            }

        }
        //---display the new SMS message---
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
    }   

}
  }
4

1 回答 1

0

您应该从Activity一项新任务开始。

Intent k = new Intent(this.getApplicationContext(), AgAppMenu.class);
k.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(k);
于 2012-08-31T07:42:53.023 回答