警报触发时如何打开自定义对话框

时间:2015-04-12 10:38:35

标签: android alarmmanager customdialog

我的应用需要设置多个闹钟 我希望在警报触发时显示自定义对话框。

我的接收器类是

    package com.example.memopad;

import java.util.zip.Inflater;








import android.app.Dialog;
import android.app.FragmentManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;



public class AlarmReceiver extends BroadcastReceiver{


     String dato;





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

             dato=intent.getStringExtra("nome");

            lanciaDialog();

            //Toast toast =Toast.makeText(context, "Oggi e' il compleanno di  " + dato , Toast.LENGTH_SHORT);
            // toast.show();





}
    public void lanciaDialog(){

        DialogFragment dialog = new CustomDialogCumple();
        dialog.show(getSupportFragmentManager(), "missiles");

    }



    }

我的CustomDialogCumple类是

package com.example.memopad;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;

public class CustomDialogCumple extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.customdialogcumple, null));
        builder.setMessage(R.string.cumple)
               /*.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {

                   }
               })*/
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
    }
}

我在dialog.show(getSupportFragmentManager(),“导弹”)中遇到问题;

我根据谷歌指南使用了Dialogfragment 你有什么暗示吗? 提前谢谢

1 个答案:

答案 0 :(得分:1)

您要做的是从BroadcastReceiver显示一个对话框 - 这在Android中是不允许的。您只能显示活动对话框。你可以做的是让BroadcastReceiver启动一个Activity,然后显示对话框。可以在this blog阅读更多信息。

正如您所看到的,如果活动当前不可见,他会发出通知,或者如果活动是(在您的情况下是显示对话框),他会发出相关信息。