0

我创建了一个广播接收器:

package ir.smspeik.sms;


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

public class ReceiveSms extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent)
    {
    //---get the SMS message passed in---
        //---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";
        }

        //---display the new SMS message---
        //Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
        //---launch the MainActivity---
        //String[] sms = str.split("-");
    //  if(sms[1] == "140")
        //{
        Intent mainActivityIntent = new Intent(context, ir.smspeik.sms.GetResponse.class);
        mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        context.startActivity(mainActivityIntent);

        //MyMenu mymen = new MyMenu();
    //  mymen.invisibleMenuItem(R.id.menusms);
        //---send a broadcast to update the SMS received in the activity---
        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("SMS_RECEIVED_ACTION");
        //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK"
        broadcastIntent.putExtra("sms", str);
        context.sendBroadcast(broadcastIntent);
        //}
    this.abortBroadcast();
    //  this.clearAbortBroadcast();
        }
        }
    }

并在我的活动中有一个菜单。我创建一个类扩展活动并创建菜单。我的所有活动都扩展了该课程。我想在调用广播接收器时隐藏一些菜单按钮。我怎样才能做到这一点?

4

1 回答 1

0

只需传递一个值来表示您要禁用某些菜单项:

    Intent mainActivityIntent = new Intent(context, ir.smspeik.sms.GetResponse.class);
    mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    mainActivityIntent.putExtra("disable menu item", true);
    context.startActivity(mainActivityIntent);

在您的活动中:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    ...
    if(getIntent().getBooleanExtra("disable menu item", false)) {
        // Disable the items here
    }
}

如果您只想单独禁用项目,则可以传递单独的附加功能。

于 2012-09-08T18:00:45.453 回答