如何在java中使用外部类的活动内部的方法

时间:2012-05-21 15:42:22

标签: java android android-activity android-context

是否可以从常规Java类调用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();

这是外部(简单java)类中代码的方法:

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);

原因是我有一个框架可以检测模拟的变化(Android Aplication),然后这个模拟会通知这个框架的变化,并决定变更是否重要。

因此,如果更改很重要,框架必须在活动中执行showMessage方法。

4 个答案:

答案 0 :(得分:0)

您应该将方法定义为public static void write(String importantChange)而不是public void write(String importantChange)

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

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

答案 1 :(得分:0)

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

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

public void write(String importanChange) {
    MyActivity.showMessage(msg);
    ...
}

答案 2 :(得分: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
                   }

     }

答案 3 :(得分: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(); 
 }