Reputation: 59
I have a thread that i want to make some calculations and after that send it to the main activity of my program. how i can do that?
the thread is
class RemindTask extends TimerTask {
public void run() {
flag = true;
System.out.println(" Hello World!");
for (int e=0; e<2; e++){
fileToReceive = fileparts.peek();
System.out.println("fileToReceive "+fileToReceive);
fileToReceive = fileparts.remove();
}
IntegerResult = 4;
flag=false;
}
I want to send the IntegerResult to the main activity. this Timer is called from the activity from here
Timer timer = new Timer();
timer.schedule(new RemindTask(), 0,30000);
and after that i need to take the IntegerResult to the main activity to the main so tha main method is
int i=0;
//IntegerResult = 4; here i need to take the result of the thread
while (!fileparts.isEmpty() ) { //&& !fileparts1.isEmpty() && (flag == false)
String[] myStringArray = new String[IntegerResult];
for (int e=0; e<IntegerResult; e++){
if (fileToReceive != null && !fileparts.isEmpty() ){
fileToReceive = fileparts.peek();
myStringArray[e] = fileToReceive;
fileToReceive = fileparts.remove();
} else {
myStringArray[e] = " ";
}
System.out.println("myStringArray["+e+"] " + myStringArray[e]);
}
Upvotes: 2
Views: 9753
Reputation: 358
There are two ways:
something like:
public static int YOUR_INT_MESSAGE = 100;
class RemindTask extends TimerTask {
public void run() {
flag = true;
System.out.println(" Hello World!");
for (int e=0; e<2; e++){
fileToReceive = fileparts.peek();
System.out.println("fileToReceive "+fileToReceive);
fileToReceive = fileparts.remove();
}
IntegerResult = 4;
flag=false;
Message msg = new Message();
msg.what = YOUR_INT_MESSAGE;
msg.obj = IntegerResult;
YourActivity.this.handler.sendMessage(msg);
}
and on your MainActivity implement the handler
public Handler handler;
handler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
if (msg.what == YOUR_INT_MESSAGE) {
int i=0;
IntegerResult = msg.obj;
while (!fileparts.isEmpty() ) { //&& !fileparts1.isEmpty() && (flag == false)
String[] myStringArray = new String[IntegerResult];
for (int e=0; e<IntegerResult; e++){
if (fileToReceive != null && !fileparts.isEmpty() ){
fileToReceive = fileparts.peek();
myStringArray[e] = fileToReceive;
fileToReceive = fileparts.remove();
} else {
myStringArray[e] = " ";
}
System.out.println("myStringArray["+e+"] " + myStringArray[e]);
}
}
}
}
I cannot test the code above right now, but it's going to be something like that, if you want the Handler approach.
Upvotes: 5
Reputation: 12446
Seems like what you need is AsyncTask
You can go with Service as Rstar proposed, but it sesms to be a little overkill.
Upvotes: 0
Reputation: 5979
Updated
Use Custom BroadcastReceiver
Write this in ActivityA.java
Intent intent = new Intent();
intent.putExtra("message","hi");
intent.setAction("com.android.activity.SEND_DATA");
sendBroadcast(intent);
Write this in ServiceA.java/ActivityB.java
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Extract data included in the Intent
String message = intent.getStringExtra("message");
Log.d("receiver", "Got message: " + message);
}
};
Now register Receiver
LocalBroadcastManager.getInstance(mContext).registerReceiver(mMessageReceiver,
new IntentFilter("com.android.activity.SEND_DATA"));
Upvotes: 1