android显示来自非UI或非活动类的警报对话框 [英] android to show alert dialog from non-UI or Non activity class

查看:86
本文介绍了android显示来自非UI或非活动类的警报对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android程序员,

Hi Android programmers,

此问题是先前提出的.但是没有答案.所以我需要解决方案.实际上,我正在尝试显示从test.java调用的Mainactivity.java警报框(非活动状态).

This question is previously asked. But there is no answer. So i need solution for that. Actually i am trying to display alert box of Mainactivity.java calling from test.java(Non-activity).

如果它们不是UI编码,则可以正常工作.如果该代码依赖于UI,则表示其抛出错误.

Thats working fine if their is no UI coding. if that code is dependent with UI means its throw errors.

活动类别:

public class Mainactivity extends Activity
{
    public void message()
    {
        Log.i("Success : Call from non-UI & non activity class");//Upto this line working if i called message() function from other non-activity class
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener()
        {
        public void onClick(DialogInterface dialog, int which)
        {
             switch (which)
             {
                //Code
            }
         }
     };
     AlertDialog.Builder builder = new AlertDialog.Builder(this);
     builder.setTitle("FMS Status Message :- ");
     builder.setMessage(msg)
     .setPositiveButton("OK",dialogClickListener).show();

    }
}

非活动类:

public class test extends BroadcastReceiver
{
    Mainactvity in = new Mainactvity();   

    @Override   
    public void onReceive(Context context, Intent intent)
    {
       in.message();
    }
}

如果解决了,那么我项目的主要目标将完成.请你们中的任何一个帮助我.

If it is resolved then my main objective of my project will be completed. Please any one of you help me.

谢谢.

推荐答案

首先,Android的不良编程实践,

First of All, Bad Programming Practice for Android,

Mainactvity in = new Mainactvity();   

您不能创建Activity的构造方法类.

You can not make a Constructor of Activity class.

第二,您尝试在BroadcastReceiver中的未显示Activity的Dialog中显示UI元素.使用Activity的 Context (如果与UI有关)或Application来在BroadcastReceiver中显示对话框.

And second, You are trying to display UI elements in Dialog which has not reference of Activity, in BroadcastReceiver. Use Context of Activity (If it concern with UI) or Application for displaying Dialog in BroadcastReceiver.

这篇关于android显示来自非UI或非活动类的警报对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆