Nov 2, 2011

Pass data from Service to Activity

Refer to the last post Create our own Service and BroadcastReceiver, data can be passed from Service using intent.putExtra(); on the other hand, the data passed can be retrieved using getIntExtra() in BroadcastReceiver.

Pass data from Service to Activity

Modify from the last post Create our own Service and BroadcastReceiver.
AndroidServiceTestActivity.java
package com.AndroidServiceTest;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;

public class AndroidServiceTestActivity extends Activity {

MyReceiver myReceiver;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

}

@Override
protected void onStart() {
// TODO Auto-generated method stub

//Register BroadcastReceiver
//to receive event from our service
myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MyService.MY_ACTION);
registerReceiver(myReceiver, intentFilter);

//Start our own service
Intent intent = new Intent(AndroidServiceTestActivity.this,
com.AndroidServiceTest.MyService.class);
startService(intent);

super.onStart();
}

@Override
protected void onStop() {
// TODO Auto-generated method stub
unregisterReceiver(myReceiver);
super.onStop();
}

private class MyReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub

int datapassed = arg1.getIntExtra("DATAPASSED", 0);

Toast.makeText(AndroidServiceTestActivity.this,
"Triggered by Service!\n"
+ "Data passed: " + String.valueOf(datapassed),
Toast.LENGTH_LONG).show();

}

}
}


MyService.java
package com.AndroidServiceTest;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class MyService extends Service {

final static String MY_ACTION = "MY_ACTION";

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub

MyThread myThread = new MyThread();
myThread.start();

return super.onStartCommand(intent, flags, startId);
}

public class MyThread extends Thread{

@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0; i<10; i++){
try {
Thread.sleep(5000);
Intent intent = new Intent();
intent.setAction(MY_ACTION);

intent.putExtra("DATAPASSED", i);

sendBroadcast(intent);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
stopSelf();
}

}

}


Note: Modify AndroidManifest.xml to add service ".MyService", refer to the post Create our own Service and BroadcastReceiver.

Next:
- Pass data from Activity to Service via Intent in startService()



6 comments:

  1. Replies
    1. Hi. I just want to know why you have used for loop in run()

      Delete
  2. Awesome Man It worked very fine for me, I have been looking for a way to do this for very long time. Thanks From Colombia

    ReplyDelete
  3. i am new on android programming, thanks for share this

    ReplyDelete
  4. Broadcast is not getting received in my case. What could be the reason?

    ReplyDelete

Infolinks In Text Ads