Android make callback to an Activity from java class

﹥>﹥吖頭↗ 提交于 2021-02-20 16:20:47

问题


How can i make a callback to an Activity form a Java Class?

Example:

    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
    }

   }

When sendToSomeActivity() is called, i want to make a callback to the already started SomeActivity and pass some text to the Activity. In SomeActivity i want to use the text.

Note: The TestClass object that i want to use is already created in another class.

How can this be done?


回答1:


The solution I chose is as follows:

Use BroadcastReceivers to communicate between Java classes and Activities.

Example:

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

}



回答2:


Try this..

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
  }
}



回答3:


In your java class create an interface like this

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

And in your activity do something like this:

TestClass tclass = new TestClass(context);

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

      //sendWhateverYouWant is here in activity

    }
});

You can also visit these links for better understanding.

How to create our own Listener interface in android?

Observer Design Pattern in Java



来源:https://stackoverflow.com/questions/29257546/android-make-callback-to-an-activity-from-java-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!