Android从java类回调一个Activity

时间:2015-03-25 13:47:30

标签: android android-activity callback

如何从一个Java类的Activity中回调?

示例:

    public class TestClass{
    String text = "Test";
    public TestClass(Context context){
            startActivity(new Intent(context, SomeActivity.class));
    }

    private void sendToSomeActivity(){
      //Call some method of SomeActivity and pas text as string
    }

   }

调用sendToSomeActivity()时,我想回调已经启动的SomeActivity并将一些文本传递给Activity。在SomeActivity我想使用该文本。

注意:我想要使用的TestClass对象已在另一个类中创建。

如何做到这一点?

3 个答案:

答案 0 :(得分:4)

我选择的解决方案如下:

使用BroadcastReceivers在Java类和活动之间进行通信。

示例:

public class SomeActivity extends Activity{
    private MyBroadcastReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        receiver = new MyBroadcastReceiver();
        this.registerReceiver(receiver, new IntentFilter(MyBroadcastReceiver.ACTION));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.unregisterReceiver(receiver);
    }

    private class MyBroadcastReceiver extends BroadcastReceiver{
        public static final String ACTION = "com.example.ACTION_SOMETHING"
        @Override
        public void onReceive(Context context, Intent intent) {
            String test = intent.getStringExtra("dataToPass");
        }
    }
} 

public class TestClass{
    private String test = "TEST";
    private Context context;

    public TestClass(Context context){
      this.context = context;
    }
    private void sendToSomeActivity(){
       Intent intent = new Intent();
       intent.setAction(SomeActivity.MyBroadcastReceiver.ACTION);
       intent.putExtra("dataToPass", test);
       context.sendBroadcast(intent); 
    }

}

答案 1 :(得分:0)

在您的java类中,像这样创建一个接口

public class TestClass{


    private MyInterface myInterface;

    public interface OnSendSomething {
        public void onSending(String sendWhateverYouWant);
    }

    public void setOnSendListener(MyInterface myInterface) {
        this.myInterface = myInterface;
    }

}


private void sendToSomeActivity(){
      //Call some method of SomeActivity and pas text as string

     myInterface.onSending(sendWhateverYouWant);
}

在你的活动中做这样的事情:

TestClass tclass = new TestClass(context);

tclass.setOnSendListener(new OnSendSomething () {
    @Override
    public void onSending(String sendWhateverYouWant) {

      //sendWhateverYouWant is here in activity

    }
});

您还可以访问这些链接以便更好地理解。

How to create our own Listener interface in android?

Observer Design Pattern in Java

答案 2 :(得分:0)

试试这个..

public class TestClass{

interface Implementable{
  public void passData(String text);
}

Implementable imple; 

String text = "Test";
public TestClass(Context context){
        startActivity(new Intent(context, SomeActivity.class));
}

private void sendToSomeActivity(){
  if(imple != null){
   imple.passData(text);
  }
}

public void setListener(Implementable im){
  imple = im;
  }

}

class SomeActivity implements Implementable{

new TestClass().setListener(this);

@override
public void passData(String text){
//here is your text
  }
}