0

我的应用程序接收 SMS 和活动更改以在我的应用程序中显示警报对话框。Toast运行良好,但不会改变活动。onReceive()接收包含电子邮件的 SMS,并根据该电子邮件 ID,我的应用程序搜索关联的联系号码并将其以回复消息的形式发回。

public void onReceive( Context context, Intent intent ) 
{
    // Get SMS map from Intent
    Bundle extras = intent.getExtras();

    String messages = "";

    if ( extras != null )
    {
        // Get received SMS array
        Object[] smsExtra = (Object[]) extras.get( "pdus" );

        // Get ContentResolver object for pushing encrypted SMS to incoming folder
        //ContentResolver contentResolver = context.getContentResolver();

        for ( int i = 0; i < smsExtra.length; ++i )
        {
            SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);

            String body = sms.getMessageBody().toString();
            String address = sms.getOriginatingAddress();

            messages += "SMS from " + address + " :\n";                    
            messages += body + "\n";

            // Here you can add any your code to work with incoming SMS
            // I added encrypting of all received SMS 


        }

        // Display SMS message
        Toast.makeText( context, messages, Toast.LENGTH_SHORT ).show();
        Intent i=new Intent(context,AlertActivity.class);
       // context.startActivity(i);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    }
4

1 回答 1

2

addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)在启动 AlertActivity 活动后添加。使用这种方式:

Intent i=new Intent(context,AlertActivity.class);

i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

context.startActivity(i);
于 2012-04-04T10:45:48.180 回答