C2DM在接收时显示AlertDialog

时间:2011-07-22 10:39:21

标签: android alertdialog android-c2dm

我有一个C2DM的工作实现,如果我处理我用Toast收到的每条消息。

相反,我想使用警报对话框,用户说好,以便确认消息。

我为此编写的代码在show()行生成异常。我希望问题可能与使用的上下文(?)有关。

AlertDialog.Builder alertbox = new AlertDialog.Builder(context);
alertbox.setMessage("Received Push Notification");
alertbox.setNeutralButton("OK", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface arg0, int arg1) {
    // TODO Auto-generated method stub

    } });
    AlertDialog alert = alertbox.create();
    alert.show();

全班(根据评论要求):

public class MyC2DM 

extends BroadcastReceiver {
    public MyC2DM()
    {


    }

    @Override
    public void onReceive(Context context, Intent intent)
        {
            Log.i("Recieve","2");
            //Toast.makeText(context, "Intent Receive!", Toast.LENGTH_SHORT).show();
            if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION"))
            {
                handleRegistration(context, intent);
            } 
            else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) 
            {
                handleMessage(context, intent);
            }
         }

        private void handleRegistration(Context context, Intent intent)
        {

            String registration = intent.getStringExtra("registration_id"); 
            if (intent.getStringExtra("error") != null)
            {
                Toast.makeText(context, "Reg Error!", Toast.LENGTH_LONG).show();
                // Registration failed, should try again later.
            } 
            else if (intent.getStringExtra("unregistered") != null)
            {
                // unregistration done, new messages from the authorized sender will be rejected
                Toast.makeText(context, "Unreg!", Toast.LENGTH_LONG).show();
            } 
            else if (registration != null) 
            {
               // Send the registration ID to the 3rd party site that is sending the messages.
               // This should be done in a separate thread.
               // When done, remember that all registration is done.

            //  UserId = customer.getId();
            //  Log.i("id",String.valueOf(UserId));

                String RegId = registration; 
                Log.i("reg",String.valueOf(RegId) );
            }
        }

        private void handleMessage(Context context, Intent intent)
        {
            // Message handler.
             Log.i("Recieve","MESSAGE");

             AlertDialog.Builder alertbox = new AlertDialog.Builder(context);
             alertbox.setMessage("Received Push Notification");

             alertbox.setNeutralButton("OK", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface arg0, int arg1) {
                    // TODO Auto-generated method stub

                } });
             AlertDialog alert = alertbox.create();
             alert.show();

             /*Toast.makeText(context, "ALERT: " + intent.getStringExtra("payload"), Toast.LENGTH_LONG).show();*/

        }
}

2 个答案:

答案 0 :(得分:0)

无法显示来自BroadcastReceiver和Service.Use Notification的Dialog

答案 1 :(得分:0)

无法在BroadcastReceiver中显示对话框。我有同样的问题。我创建了一个Activity,它从android:style / Theme.Dialog继承了它的样式。这使得Activity看起来像一个Dialog。