从广播接收器获取活动的意图

时间:2013-09-28 18:58:08

标签: android

我希望我的activity class收到来自broascast Receiver的意图(示例START_TALKINGSTOP_TALKING)。当我收到intent时,我想查看正在通过的操作。我怎样才能做到这一点。我的接收器是在单独的类中,它是public

这是我的代码

public void onReceive(Context context, Intent intent)
{
    if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
        KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
                switch (event.getKeyCode()) {
    case KeyEvent.KEYCODE_HEADSETHOOK:
        if (action == KeyEvent.ACTION_DOWN)
            // here I want to notify my activity class (e.g.      startActivity? I don't know)
        break;
    case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
        // here I want to notify my activity class (e.g.      startActivity? I don't know)
              }
    }
}

我真的需要你帮助的人tnx。

3 个答案:

答案 0 :(得分:1)

这是我的解决方案,在我的项目中,希望它能帮到你:

you should type this:
// put your action string in intent
Intent intent = new Intent("com.example.myproject.ADD_ITEM_BASKET");
// start broadcast
activity.sendBroadcast(intent);


public class Myclass extends Fragment {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // creating and register receiver
    Receiver receiver = new Receiver();
    IntentFilter intentFilterAdd = new IntentFilter("com.example.myproject.ADD_ITEM_BASKET");
    IntentFilter intentFilterEdit = new IntentFilter("com.example.myproject.DELETE_ITEM_BASKET");


    getActivity().registerReceiver(receiver, intentFilterAdd);
    getActivity().registerReceiver(receiver, intentFilterDelete);

}
    // your receiver class 
    class Receiver extends BroadcastReceiver
    {

        // catch messages from intent
        @Override
        public void onReceive(Context context, Intent intent) {

            if("com.example.myproject.ADD_ITEM_BASKET".equals(intent.getAction().toString()))
            {

                // do something
            }
            else if("com.example.myproject.DELETE_ITEM_BASKET".equals(intent.getAction().toString()))
            {
                // do something

            }


        }
    }

}

答案 1 :(得分:0)

您必须注册接收器......请遵循此示例..

public class MyActivity extends Activity {

private BroadcastReceiver myBroadcastReceiver =
    new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // your onreceive code here
        }
   });

...

public void onResume() {
    super.onResume();
    ....
    registerReceiver(myBroadcastReceiver, intentFilter);
}

public void onPause() {
    super.onPause();
    ...
    unregisterReceiver(myBroadcastReceiver);
}
...
}

答案 2 :(得分:0)

您可以在BroadcastReceiver的onReceive()中使用putExtra()。

/**
 * @author Skylifee7 on 23/06/2017.
 * TemplateResultReceiver.java
 */

public class TemplateResultReceiver extends BroadcastReceiver {

        private static final String TAG = "BleshTemplate";
        public static final String EXTRA_MESSAGE = "TRANSACTION_MESSAGE";
        Context mContext;

        @Override
        public void onReceive(Context context, Intent intent) {

            mContext = context;

            if (intent.getAction().equals(BleshConstant.BLESH_TEMPLATE_RESULT_ACTION)) {

                String actionType = intent.getStringExtra(BleshConstant.BLESH_ACTION_TYPE);
                String actionValue = intent.getStringExtra(BleshConstant.BLESH_ACTION_VALUE);

                if (actionType != null && actionValue != null) {

                    switch (actionType) {
                        case "MENU": sendMessage(actionValue);
                        /*
                        You may want to increase the case possibilities here, like below:
                        case: "ADMOB"
                        case: "VIRTUAL_AVM"
                        case: "SMART_CAR_KEY"
                         */
                        default: sendMessage(actionValue);
                    }
                }
            }
    }

    private void sendMessage(String actionValue) {
        Intent intent = new Intent(mContext.getApplicationContext(),TransactionActivity.class);
        intent.putExtra(EXTRA_MESSAGE, actionValue);
        mContext.getApplicationContext().startActivity(intent);
    }
}

在您的Activity类'onCreate()方法中:

/**
 * @author Skylifee7 on 24/06/2017.
 * TransactionActivity.java
 */

public class TransactionActivity extends AppCompatActivity   {

    private String bleshKey;
    private String TAG = "Transaction_Activity";
    private String amount;
    private String isSuccessful; //may be cast to boolean type.
    private double latitude, longitude;

    private LocationRequest mLocationRequest;
    protected GoogleApiClient mGoogleApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payment);

        requestPermission();
        initLocationService();
        // Get the Intent that started this activity and extract the string
        Intent intent = getIntent();
        bleshKey = intent.getStringExtra(BleshTemplateResultReceiver.EXTRA_MESSAGE);

        ImageButton paymentBtn = (ImageButton) findViewById(R.id.buttonPay);
        final EditText editTextAmount = (EditText) findViewById(R.id.editTextAmount);

        paymentBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                amount = editTextAmount.getText().toString();
                callApiService();
            }
        });
    }
}