I want to catch a broadcast intent, then forward the details of the intent to a specific Activity in my app.
So in my manifest file, I have this:
<receiver android:name="uk.floriansystems.carplayer.MediaButtonReceiver">
<intent-filter android:priority="999">
<action android:name="android.intent.action.MEDIA_BUTTON"></action>
</intent-filter>
</receiver>
The BroadcastReceiver is this:
public class MediaButtonReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
KeyEvent keyEvent = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
int keyEventKeyCode = keyEvent.getKeyCode();
int keyEventAction = keyEvent.getAction();
// I want to pass the above integers to my Activity, within the Intent
// created below, in a tidy way:
context.startActivity(new Intent(context, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
}
I want the activity to receive the keyEvent's action and keyCode via the intent that explicitly invokes the activity.
Is there a way I can re-use or clone the original broadcast intent, modifying it as minimally as possible before sending it to the Activity? Or are there similar alternatives for tidy/good practice ways of relaying the information to the Activity?
What I really want to do is maximise flexibility, so for example, it would (IMO) not be good if either:
Should I just copy the action, category, data and extras from the first intent to the second? Are there any others bits I could/should copy?
Try using the copy constructor:
Intent activityIntent = new Intent(intent);
activityIntent.setClass(context, MainActivity.class);
context.startActivity(activityIntent);