单击android中通知内的按钮打开对话框

vid*_*dzy 12 notifications android dialog

正如您在通知中看到的批准/拒绝按钮,我想打开一个对话框以确认用户输入而不打开任何活动.

在此输入图像描述

这是我的代码,其中MyDialog是一个Activity,但我没有打开这个活动,而是想打开一个对话框.

public void createNotification(View view) {

    Intent yesIntent = new Intent(this, MyDialog.class);
    yesIntent.putExtra("NOTI_ID", NOTIFICATION_ID);
    yesIntent.putExtra("ACTION", 1);
    PendingIntent yesPIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), yesIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Intent noIntent = new Intent(this, MyDialog.class);
    noIntent.putExtra("ACTION", 0);
    noIntent.putExtra("NOTI_ID", NOTIFICATION_ID);
    PendingIntent nopIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), noIntent, 0);



    NotificationCompat.Builder noti = new NotificationCompat.Builder(this)
            .setContentTitle("New Project Approval")
            .setContentText("Project Description")
            .setContentIntent(PendingIntent.getActivity(MainActivity.this, 0, yesIntent, PendingIntent.FLAG_CANCEL_CURRENT))
            .setSmallIcon(R.mipmap.bell)
            .setAutoCancel(true)
            .addAction(R.mipmap.approve_ic, "Approve", yesPIntent)
            .addAction(R.mipmap.rejecticon, "Reject", nopIntent) ;

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, noti.build());

}
Run Code Online (Sandbox Code Playgroud)

Sah*_*nda 5

如果您想在不显示活动的情况下打开对话框。考虑以下

1.创建一个活动并将其Manifest值设置为

<activity android:name=".MyDialog"
            android:launchMode="singleInstance" android:excludeFromRecents="true"
            android:taskAffinity="" android:theme="@style/Theme.AppCompat.Dialog">
        </activity>
Run Code Online (Sandbox Code Playgroud)
  1. 在此活动的 oncreate 方法中。使用以下构建器创建并显示对话框

     AlertDialog LDialog = new AlertDialog.Builder(this)
                .setTitle("Title")
                .setMessage("Message")
                .setOnCancelListener(this)
                .setOnDismissListener(this)
                .setPositiveButton("ok", null).create();
        LDialog.show();
    
     @Override
        public void onCancel(DialogInterface dialogInterface) {
            if(!MyDialog.this.isFinishing()){
                finish();
            }
        }
    
        @Override
        public void onDismiss(DialogInterface dialogInterface) {
            if(!MyDialog.this.isFinishing()){
                finish();
            }
        }
    
    Run Code Online (Sandbox Code Playgroud)

现在使用createNotification(View view)函数生成通知。

在此处输入图片说明


小智 1

您还可以从挂起的意图打开活动并使用半透明主题。并从该活动打开对话框

public class OffersDialogActivity extends BaseActivity {
    private AlertDialog alertDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);

    }
    @Override
    protected void onResume() {
        super.onResume();
        setUpDialog();
    }

    private void setUpDialog() {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        LayoutInflater inflater = this.getLayoutInflater();

        dialogBuilder.setView(dialogView);
        alertDialog = dialogBuilder.create();
        alertDialog.setCancelable(false);
        alertDialog.setCanceledOnTouchOutside(false);
        if(!isFinishing())
        {
            alertDialog.show();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if(alertDialog != null){
            alertDialog.dismiss();
        }
        finish();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(alertDialog != null) {
            alertDialog.dismiss();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

并使用主题:

<style name="TransparentTheme" parent="@style/NoActionBarTheme">
        <item name="android:background">@null</item>
        <item name="background">@null</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowAnimationStyle">@null</item>
        <item name="android:windowNoTitle">true</item>
    </style>
Run Code Online (Sandbox Code Playgroud)