作为BroadcastReceiver对话的活动,来自另一个活动

时间:2016-03-12 04:29:55

标签: android android-layout android-intent android-activity broadcastreceiver

我的应用会在显示该应用中的Intent时收听由第三方应用触发的Activity。我的应用中的Intent收到了BroadcastReceiver。我想从BroadcastReceiver开始一个活动,该活动将显示为现有Dialog activity(激发意图)。

@Override
public void onReceive(final Context context, Intent intent) {
    String action = intent.getAction();
    Log.d(TAG, ">>>>>>>>> Action:" + action);

    if ("clover.intent.action.V1_ORDER_BUILD_START".equals(action)) {
        Intent i = new Intent(context.getApplicationContext(), ActiveOrderActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

Intent clover.intent.action.V1_ORDER_BUILD_START是由我的应用程序侦听的其他应用程序触发的。触发此Intent时,Activity已经打开(请参见下图中的背景活动)。

现在我想在我的应用程序中显示一个活动,作为对已经显示的活动的Dialog,就像下图中的“添加订单客户”一样。

如上面的代码所示,我从BroadcastReceiver开始一个Activity,但是当它开始时,它会进入前台并且之前的Activity不会显示。

请参阅下文,了解我想要实现的目标,

Example UI of what i would like to achieve

1 个答案:

答案 0 :(得分:1)

也许你应该创建

public class MyDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Get the layout inflater
    LayoutInflater inflaterViewObject = LayoutInflater.from(getActivity());
    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    final View DialogView = inflaterViewObject.inflate(R.layout.dialog, null);
    final AlertDialog Dialog = new AlertDialog.Builder(getActivity()).create();
    Dialog.setView(DialogView);

    DialogView.findViewById(R.id.dialog_YES).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //your YES logic
            Dialog.dismiss();
        }
    });

    DialogView.findViewById(R.id.dialog_NO).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        //Your NO LOGIC
            Dialog.dismiss();
        }
    });

    // return dialog object (later on .show());
    return Dialog;
}

稍后你会在你选择的地方写字(在BrodcastReciever中)

MyDialog dialogObject = new MyDialog();
dialogObject.show(getFragmentManager(), "tag name for the dialog fragment.");