0

Is it possible to call a method of Activity from the regular Java class?

This is the method in the activity:

public void showMessage(String mensaje) {
   AlertDialog.Builder builder = new AlertDialog.Builder(this);
   builder.setMessage("Are you sure you want to exit?")
          .setCancelable(false)
          .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                   dialog.cancel();
              }
          })
          .setNegativeButton("No", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                   dialog.cancel();
              }
          });
   AlertDialog alert = builder.create();
   alert.show();

This is the method of the code in the external (simple java) class:

public void write(String importanChange) {
    Context context = //Here I dont know what to do here (It's my question,
                  //but I tried other aproaches but all failed                     
    ((MyActivity)mContext).showMessage(message);

The reason is that I have a framework that detects changes on a simulation (Android Aplication) and later this simulation notifies to this framework changes and it decides whether the change is important or not.

So if the change is important the framework must execute the showMessage method in the activity.

4

4 回答 4

0

您应该将您的方法定义public static void write(String importantChange)public void write(String importantChange)

将您的方法设置为“静态”意味着您不必实例化包含您的方法的类来使用此方法。

编辑:我刚刚看到你的“编辑”。因此,要在您的方法中使用 Context,我建议您将上下文作为方法的参数传递,如下所示:public void write(String importantChange, Context context)

于 2012-05-21T15:45:42.437 回答
0

由于AlertDialog不需要,Context您可以选择创建实用方法:

public static void showMessage(String message) {
    ...
}

public void write(String importanChange) {
    MyActivity.showMessage(msg);
    ...
}
于 2012-05-21T16:11:30.693 回答
0

尝试这个,

例如:

Activity Class:


      public class MyActivity extends Activity {

                    onCreate(.....){

               }

         public void go(){

         //Some code

     }



Java External Class in the same package:


public class MyDemo {

     MyActivity ac;
                   public MyDemo(MyActivity ac) {


                           this.ac = ac;
                     }

                 public void foo() {

                          ac.go();    // Access the activity's method
                   }

     }
于 2012-05-21T18:53:54.113 回答
0
public static void showMessage(String importantMensaje){         
        final String mensajeAMostrar = importantMessage;
        Runnable runnable = new Runnable() {
            public void run() {
                handler.post(new Runnable() { // This thread runs in the UI
                    public void run() {
                        MyActivity.builder.setMessage(mensajeAMostrar)
                        .setCancelable(false)
                        .setPositiveButton("Of Course", new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog, int id) {
                              dialog.cancel();
                         }
                        });
                        AlertDialog alert = MyActivity.builder.create();
                        alert.show();
                    }
                });
            }
        };
        new Thread(runnable).start(); 
 }
于 2012-05-23T10:06:34.237 回答