Wait in activity? - android

I have a class that has a service:
private ServiceConnection conn = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
service = ((MyService.LocalBinder)service).getService();
isBound = true;
}
#Override
public void onServiceDisconnected(ComponentName name) {
service = null;
isBound = false;
}
};
I also have this method:
public boolean isBound() {
return isBound;
}
In my activity I create an instance of the class. However, the service does not connected immediately.
In the activity, how can I wait the isBound() returns true without blocking the activity?

I am afraid you can't. By the way, you can also notify the world that your service has correctly connected with a BroadcastReceiver

In your service class send broadcast:
Intent intent=new Intent();
intent.setAction("com.intent.service.connected");
sendBroadcast(intent);
In your activity catch the intent:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter("com.intent.service.connected");
this.registerReceiver(new Receiver(), filter);
}
private class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
// your codes
}
}

Related

How to keep polling a bound service?

I looked up on the internet, but couldn't find an example covering my scenario. What I am trying to do is:
1) To start and bind to a service as soon as my activity starts (done)
2) The service then binds itself to another service looking for a user
input from a connected device, and saves a string a string to a variable (done)
3) I would like to send back this string to the activity, so I can check what it
is and based on it to make a network call.
Now number 3) is my challenge. I managed to do it with a Timer that runs for one second and then checks the value written in the service, but somehow this doesn't seem to be the right way and I think that there might be a more mature solution. However, I can't seem to figure it out.
I've taken the code from the documentation and only added the timer. It is just one service in this example that just generates a random number (this will normally be replaced by my second service).
This is the code for the service:
public class LocalService extends Service {
private final IBinder mBinder = new LocalBinder();
private final Random mGenerator = new Random();
public class LocalBinder extends Binder {
LocalService getService() {
return LocalService.this;
}
}
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public int getRandomNumber() {
return mGenerator.nextInt(100);
}
}
And this is the code in my activity:
public class MainActivity extends AppCompatActivity {
LocalService mService;
boolean mBound = false;
Timer timer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer = new Timer();
}
#Override
protected void onStart() {
super.onStart();
Intent intent = new Intent(this, LocalService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
timer.schedule(new MyTimerTask(new Handler(), this), 1000, 1000); // run on every second
}
#Override
protected void onStop() {
super.onStop();
if (mBound) {
unbindService(mConnection);
mBound = false;
}
timer.cancel();
timer.purge();
}
private class MyTimerTask extends TimerTask {
Handler handler;
MainActivity ref;
public MyTimerTask(Handler handler, MainActivity ref) {
super();
this.handler = handler;
this.ref = ref;
}
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
if (mBound) {
int num = ref.mService.getRandomNumber();
// just as an example, raise a toast to see if it works
// but otherwise the value will be handled
Toast.makeText(ref, "number: " + num, Toast.LENGTH_SHORT).show();
}
}
});
}
}
private ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className,
IBinder service) {
LocalService.LocalBinder binder = (LocalService.LocalBinder) service;
mService = binder.getService();
mBound = true;
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
}
My question is: is this a good approach (it works) or is it bad and what's the alternative?
You can use LocalBroadcastManager to send broadcasts from your Service to your Activity. For example, in your Service declare:
public static final String BROADCAST_INTENT = "broadcast_intent";
public static final String BROADCAST_VALUE = "broadcast_value";
private LocalBroadcastManager broadcastManager;
public void onCreate() {
super.onCreate();
broadcastManager = LocalBroadcastManager.getInstance(this);
}
Now whenever you want to send a String to your Activity you can do so like this:
private void sendBroadcast(String value) {
Intent intent = new Intent(BROADCAST_INTENT);
intent.putExtra(BROADCAST_VALUE, value);
broadcastManager.sendBroadcast(intent);
}
In your Activity declare a BroadcastReceiver:
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
handleIntent(intent);
}
};
Register the receiver when you bind to your Service:
IntentFilter broadcastIntentFilter = new IntentFilter();
broadcastIntentFilter.addAction(StreamService.BROADCAST_INTENT);
LocalBroadcastManager.getInstance(context).registerReceiver((broadcastReceiver), broadcastIntentFilter);
And unregister where you unbind from your Service:
LocalBroadcastManager.getInstance(context).unregisterReceiver(broadcastReceiver);
Now when your service sends the broadcast you can handle it in your Activity:
private void handleIntent(Intent intent) {
if (intent.getAction().equals(StreamService.BROADCAST_INTENT)) {
String value = intent.getStringExtra(StreamService.BROADCAST_VALUE, "default");
}
}
I would like to send back this string to the activity, so I can check what it is and based on it to make a network call.
Use LocalBroadcastManager, greenrobot's EventBus, Square's Otto, or some other in-process event bus implementation. Raise an event when you have changed data. Have the activity register with the bus to find out about the event. Have the activity use the changed data when the change occurs.
is this a good approach
No.

BroadcastReceiver to send notification and update ui if activity, active

I have an activity which just displays a daily counter. When the day ends, i want to send a notification with last day counter, insert it in a db and if the app is running update the counters label to zero.
I think i must register a static <receiver>android.intent.action.DATE_CHANGED</receiver>. so as to get notified even if the activity is not running.
There I insert the value in DB and i send the notification.
And a dynamic created broadcast receiver which i will unregister onPause, which receives the same events, but it will only update the UI and specifically the label.
Is this the best solution?
Is there any way from a broadcastReceiver to make my Activity (if is running) to go to resume again ?
Is it possible from broadcastReceiver to call a Service, which will update my UI if my Activity is running?
Use broadcastreceiver that would catch the event
public class EventReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
NotificationService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
broadcastreceiver give event service
public class NotificationService extends IntentService {
private final ServiceBinder binder = new ServiceBinder();
private ServiceListener serviceListener;
public NotificationService() {
super("NotificationService");
}
#Override
public IBinder onBind(Intent intent) {
return binder;
}
#Override
public boolean onUnbind(Intent intent) {
serviceListener = null;
return true;
}
#Override
public void onRebind(Intent intent) {
super.onRebind(intent);
}
#Override
protected void onHandleIntent(Intent intent) {
//send notification
if(serviceListener != null){
serviceListener.updateActivity();
}
}
public void setServiceListener(ServiceListener serviceListener) {
this.serviceListener = serviceListener;
}
public class ServiceBinder extends Binder {
public NotificationService getSevice(){
return NotificationService.this;
}
}
public static interface ServiceListener{
public void updateActivity();
}
}
then you need to connect to the service in your activity
public class MainActivity extends Activity implements NotificationService.ServiceListener {
private boolean isBound;
private ServiceConnection serviceConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
NotificationService.ServiceBinder binder = ((NotificationService.ServiceBinder)service);
binder.getSevice().setServiceListener(MainActivity.this);
isBound = true;
}
#Override
public void onServiceDisconnected(ComponentName name) {
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
protected void onResume() {
super.onResume();
if(!isBound) {
Intent intent = new Intent(this, NotificationService.class);
bindService(intent, serviceConnection, BIND_AUTO_CREATE);
}
}
#Override
protected void onPause() {
super.onPause();
if(isBound){
unbindService(serviceConnection);
isBound = false;
}
}
#Override
public void updateActivity() {
runOnUiThread(new Runnable() {
#Override
public void run() {
//update you list here
}
});
}
}
and do not forget to mention the service and broadcastreceiver in the manifest file and specify the intent filter
<receiver android:name="EventReceiver">
<intent-filter>
<action android:name="........." />
</intent-filter>
</receiver>
<service android:name="NotificationService"/>
You can simply use a LocalBroadcastReceiver to send events to your Activity , a working example could be found here
After more reading i found that the best solution is the above.
A static broadcast receiver for DATE_CHANGED actions. This will start a Service which will update the db and send a local custom broadcast (com.myBroadcast.WakeUpActivity)
The Activity if is running will register a receiver for the previous custom broadcast (will unregister onPause), and will update the UI.

Is it possible to use AIDL without binding service to the activity

I've implemented and aidl file as follows:
interface ITaskService{
void addGetNamesJob();
void addUploadJob();
}
I then in my service have done the following:
#Override
public IBinder onBind(Intent intent) {
return new ITaskService.Stub() {
#Override
public void addBeerNamesJob() throws RemoteException {
jobManager.addJobInBackground(new GetNamesTask());
}
#Override
public void addUploadJob() throws RemoteException {
jobManager.addJobInBackground(new UploadRatingTask(wifi, twitter, TWITTER_CONSUMER, TWITTER_CONSUMER_SECRET, /*ctx,*/ accessToken, accessTokenSecret ));
}
};
}
then in my main activity the service is bound as follows:
private void initService() {
Log.i(TAG, "initService()" );
mServiceConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
service = ITaskService.Stub.asInterface((IBinder)iBinder);
}
#Override
public void onServiceDisconnected(ComponentName componentName) {
bound = false;
service = null;
}
};
if(service == null){
Intent it = new Intent();
it.setAction("com.company.taskservice");
bindService(it, mServiceConnection, Context.BIND_AUTO_CREATE);
bound = true;
}
}
private void releaseService() {
if(bound && mServiceConnection!=null && service!=null){
unbindService(mServiceConnection);
}
bound = false;
service = null;
Log.d(TAG, "releaseService(): unbound.");
}
This all works perfectly, I can call on the methods in the service via the interface. However, what I would like to do now is on device boot is start the service, which I written like this:
public class DeviceBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent ratingUpload = new Intent(context, RatingUploaderService.class);
//Toast.makeText(context, "------------->>>>DEVICE BOOT RECEIVER CALLED ---- CONNECTED", Toast.LENGTH_LONG).show();
context.startService(ratingUpload);
}
}
}
I don't want the service to die if the app is A. sent to the background or B. killed. So if it's possible to start the service on boot, it will run independently of the application. However, can I through my aidl file access the methods without binding and if so can someone show me how?
thanks in advance

Update service running on background

I need to implement such a procedure:
Start a background service
Update the service with parameters (from UI - user input)
After activity ended the service should keep on running and preform requests to HTTP server every minute. in this stage i still need the parameters I updated in the second stage - I send them to the server.
The service should store the server last response and compere each with the last. if there is a change, notify the user.
Finally, when the activity starts again, the service should update UI with latest the server response.
What I tried:
BroadcastReciver - The problem is after onRecive ended all the arguments which aren't declared as final will wipe out, as well as I didn't found a way to update the Intent being sent automatically every minute.
Service - Using startService() - The problem is when the activity ended the service like stops and starts , flushing all it's arguments. and once again I didn't figured out how to update the arguments after the service is already started.
So how to handle such a situation?
Thanks.
It sounds like what you need to do is to be able to "bind" to your service. What I have posted below is a simple template of how to do that. For your purposes you will need to store variables in your Service class and create getters so that when you re-launch your activity you can get the most up to date variables. Also - please note that I start and stop the Service example below in onResume and onPause. You will no doubt want to do this differently.
//Activity
//Bind to Service Example
public class ExampleActivity extends Activity implements OnClickListener {
// UI
private Button binderButton;
// service
private MyService myService;
private Intent serviceIntent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
// binder button
binderButton = (Button) findViewById(R.id.button1);
binderButton.setOnClickListener(this);
binderButton.setText("start");
serviceIntent = new Intent(this, MyService.class);
}
private ServiceConnection serviceConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
myService = ((MyService.MyBinder) service).getService();
}
#Override
public void onServiceDisconnected(ComponentName name) {
myService = null;
}
};
#Override
protected void onResume() {
super.onResume();
// start the service
startService(serviceIntent);
// bind to the service
bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
// call method within the service
myService.doServiceStuff();
break;
}
}
#Override
protected void onPause() {
super.onPause();
stopService(serviceIntent);
unbindService(serviceConnection);
}
}
//Service
public class MyService extends Service {
private final IBinder binder = new MyBinder();
#Override
public IBinder onBind(Intent arg0) {
return binder;
}
public void doServiceStuff() {
task.execute();
}
// create an inner Binder class
public class MyBinder extends Binder {
public MyService getService() {
return MyService.this;
}
}
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
Log.d("yourTag", "long running service task");
return null;
}
};
}
Thanks javaJoe, although your answer didn't solved my problem it gave me some a good ideas.
What I did:
in the Activity onCreate, check if my service is running, if so bind it else, create new one and bind it.
Transferring arguments between the Service and the Activity using setters and getters.
in the Activity onDestroy (the problem was that the service calls self Destory) the Activity sends the final arguments through Intent to a Broadcastreciver. The Broadcastreciver than starts the Service again, initiating it with the correct arguments.
I don't know if this architecture is ideal, i'd like to get some feedback.
Here is the code:
Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Set Service Intent
serviceIntent = new Intent(this, UpdateService.class);
if (isMyServiceRunning()) {
//Bind to the service
bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}else{
updateService=new UpdateService();
//Start the service
startService(serviceIntent);
//Bind to the service
bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}
}
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (UpdateService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
private ServiceConnection serviceConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
updateService = ((UpdateService.MyBinder) service).getService();
//Set Initial Args
updateService.setParams(int arg0);
}
#Override
public void onServiceDisconnected(ComponentName name) {
updateService = null;
}
};
#Override
protected void onDestroy() {
//UnBind from service
unbindService(serviceConnection);
//Stop Service
stopService(serviceIntent);
//Prepare intent to broadcast reciver
Intent intent = new Intent(MainActivity.this,ServiceRunnerBCR.class);
intent.setAction(ServiceRunnerBCR.ACTION_SET_UpdateService);
intent.putExtra(ServiceRunnerBCR.keyVal_arg0, arg0);
intent.putExtra(ServiceRunnerBCR.keyVal_arg1, arg1);
//Send broadcast to start UpdateService after the activity ended
sendBroadcast(intent);
super.onStop();
}
Broadcastreciver:
public class ServiceRunnerBCR extends BroadcastReceiver {
public static final String ACTION_SET_UpdateService = "ACTION_ALARM";
public static final String keyVal_arg0="ARG0";
public static final String keyVal_arg1="ARG1";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_SET_UpdateService)){
updateIntent(context, intent.getDoubleExtra(keyVal_arg0, 0.02), intent.getStringExtra(keyVal_arg1));
}
}
private void updateIntent(Context context, double arg0, String arg1){
Intent intent = new Intent(context,UpdateService.class);
intent.setAction(ACTION_SET_UpdateService);
intent.putExtra(keyVal_arg0, arg0);
intent.putExtra(keyVal_arg1, arg1);
synchronized (this){
try {
this.wait(6000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
context.startService(intent);
Log.d("OREN","ServiceRunner");
}
}
Service:
public class UpdateService extends Service {
private final IBinder binder = new MyBinder();
public static final String keyVal_arg0="ARG0";
public static final String keyVal_arg1="ARG1";
private Timer timer;
private HTTPHandler http = new HTTPHandler();
private int test=0;
double arg0=0;
String arg1= "";
private TimerTask updateTask = new TimerTask() {
#Override
public void run() {
test++;
Log.d("OREN", "Timer task doing work " + test + " arg0: " + arg0);
//Do some work here
}
};
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent!=null){
arg0=intent.getDoubleExtra(keyVal_arg0, 0.002);
arg1=intent.getStringExtra(keyVal_arg1);
timer = new Timer("UpdateTimer");
timer.schedule(updateTask, 1000L, 10 * 1000L);
Log.d("OREN", "ServiceStarted" + test);
}
return super.onStartCommand(intent, flags, startId);
}
#Override
public IBinder onBind(Intent intent) {
Log.d("OREN", "OnBind" + test);
return binder;
}
public void setArg0(double d){
arg0=d;
}
// create an inner Binder class
public class MyBinder extends Binder {
public UpdateService getService() {
return UpdateService.this;
}
}
#Override
public void onDestroy() {
Log.d("OREN", "OnDestroy" + test);
super.onDestroy();
}
#Override
public boolean onUnbind(Intent intent) {
Log.d("OREN", "OnUnBind" + test);
return super.onUnbind(intent);
}
}

Passing parameter from an activity to a thread in a service

I have an activity in which I start a service, for example I staty MyService as:
Intent intent1 = new Intent(this, MyService.class);
startService(intent1);
Inside my service I create a thread and run it. Here is part of my code:
public class MyService extends Service {
...
#Override
public void onStart(Intent intent, int startid) {
Thread mythread= new Thread() {
#Override
public void run() {
while(true)
{
...
}
}
};
mythread.start();
}
}
Now instead of while(true) I want to use while(a), where a is a parameter that is passed from my activity to this service. Please note that my activity is a different class than my service. How can this be done? Please show specific example with some codes.
You can get access to your service by binding to it. Edit your service class so that it returns an IBinder onBind()
public class MyService extends Service {
private static final String TAG = MyService.class.getSimpleName();
private final IBinder binder = new ServiceBinder();
private boolean a;
#Override
public IBinder onBind( Intent intent ) {
return binder;
}
#Override
public int onStartCommand( Intent intent, int flags, int startId ) {
return super.onStartCommand( intent, flags, startId );
}
#Override
public void onCreate() {
super.onCreate();
}
public class ServiceBinder extends Binder {
public MyService getService() {
return MyService.this;
}
}
public void setA(boolean a) {
this.a = a;
}
}
Now in your activity you need to handle binding and unbinding to your service. In this example, the service sticks around whether you are bound or not. If this is not the functionality you want, you can just not call startService(...):
public class MyActivity extends Activity {
//...
private MyService myService;
private boolean bound;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent( this, MyService.class );
startService( intent );
doBindService();
}
private final ServiceConnection serviceConnection = new ServiceConnection() {
public void onServiceConnected( ComponentName className, IBinder service ) {
myService = ( (MyService.ServiceBinder) service ).getService();
bound = true;
}
public void onServiceDisconnected( ComponentName className ) {
myService = null;
bound = false;
}
};
void doBindService() {
boolean bound = bindService( new Intent( this, MyService.class ), serviceConnection, Context.BIND_AUTO_CREATE );
if ( bound ) {
Log.d( TAG, "Successfully bound to service" );
}
else {
Log.d( TAG, "Failed to bind service" );
}
}
void doUnbindService() {
unbindService( serviceConnection );
}
}
Now you have a reference to your bound service in your activity and you can just call myService.setA(true) to set your parameter.
Instead of calling start service use bindService which allows you access to the service object.
Here is a detailed topic about it Android Doc
Once your activity is bound to your service you can call from your activity any method from your service.
you could do something like this:
.... Activity Code
mService.stopThread();
..... Service Code
public void stopThread(){
a = false;
}
Here is How I do it:
In your activity when you try to connect to the service :
Intent serviceIntent = new Intent(this, MyService.class);
bindService(serviceIntent, serviceConnection, BIND_AUTO_CREATE);
private ServiceConnection serviceConnection = new ServiceConnection(){
#Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
mService = (MyService) ((MyService.LocalBinder) arg1)
.getService();
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
}
};
And In my service :
I add this member
private LocalBinder mBinder;
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
mBinde = new LocalBinder();
}
and this class:
public class LocalBinder extends Binder {
public MyService getService() {
// Return this instance of LocalService so clients can call public
// methods
return MyService.this;
}
}
I think service binding will be overkill for your case, since you have simple interaction between the activity and your service.
As suggested, you can pass the parameters using startService. Another solution, is to use LocalBroadcast, here is an example
Regarding your thread, you might need to define it as separate class in your service not anonymous class, for example:
class MyThread extends Thread{
private boolean a = true;
public void setA(boolean a){
this.a = a;
}
public void run() {
while(a)
{
...
}
}
}
Simple Use
Intent intent1 = new Intent(this, MyService.class);
intent1.putExtra("key",value);
startService(intent1);
and retrieve it in service using
a = intent.getStringExtra("key");// or Int, ArrayList whatever
If I have understood the question correctly, this is what you need:
In the activity class, right before calling startService(), add this line:
intent1.putExtra("keyName","keyValue");
In the service, in onStartCommand():
Bundle extras = intent.getExtras();
String param = extras.getString("keyName");
param will hold your parameter.

Categories

Resources