I have a newbie question about Class/Task/Activity. I\'m coming from C so I don\'t know if it\'s a good approach to do what I need.
I\'ve created a class:
You can use Callback for this purpose.
Define some interface like
public interface MyCustomInterface(){
public void sendData(String str);
}
Now let your Activity implement this interface.
public class MyActivity implements MyCustomInterface {
@Override
public void sendData(String str){
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
recived_message.setText(str);
}
});
}
}
Now in UDPServer.java, write the following code
public class UDPServer {
private MyCustomInterface interface;
UDPServer(MyCustomInterface interface){
this.interface = interface;
}
}
Now whenever you have some data available lets say a string, you can send it like this
interface.sendData(str);
You have an A activity and B one, when you finish actions on B activity side you need it to effect A side when you come back.
Create an Instance
Class and a method that u type u need, let's say;
public interface SelectedBirthday {
void onSelectedData(String date);
}
Now we are on B side, Create an instance of your Interface Class
private SelectedBirthday mCallback;
Override
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (SelectedBirthday) activity;
} catch (ClassCastException e) {
Log.d("MyDialog", "Activity doesn't implement the ISelectedData interface");
}
}
Now upload the value you needed
String userBirth = (day + " " + month + " " + year);
mCallback.onSelectedData(userBirth);
Ok let's go to A side
Implement our Interface Class
implements SelectedBirthday
it will warn you for its method and you implemented it
@Override
public void onSelectedData(String date) {
if (!date.equals("")) {
txt_poup_age.setText(date);
//now you are free to do what you want with the value you received automaticaly
}
}
Maybe you could use a Handler, load it with some data, and then read those data from your activity. Check more infos here about handlers
You'd just pass an handler from your activity to your class, use handler.sendMessage("") inside your run method, and analyse what you receive inside your activity.
In android 4 option to do this
Intent i = new Intent(current_class.this, linked_class.class);
i.putextra("Key", value);
And get the value(suppose string value) in another class like:
String value = getIntent.getExtra("String key which you used when send value");
option 2
class A{
public static String _utfValue = "";
void sendValue(){ _utfValue = "some value"; } }
And fetch this value in your java class like:
String value = A._utfValue ;
there are many ways you can achieve this following are some
you can Google out more about these methods and chose what suits you better
In your case I would use activity as interface. The interface is stored as a static parameter inside Application class.