禁用广播接收器中的菜单项

时间:2012-09-08 17:53:12

标签: android menu android-actionbar

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

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();
        }
        }
    }

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

1 个答案:

答案 0 :(得分:0)

只需传递一个值即可表示您要禁用某些MenuItems:

    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
    }
}

如果您只想个别禁用项目,可以传递个别附加内容。