Update UI with background service - android

I've declared a Service class:
public class MyService extends Service{
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Parsing().execute();
return Service.START_STICKY;
}
private class Parsing extends AsyncTask<Void, Void, Void> {
List<MyObject> myList = null;
MyAdapter adapter = null;
#Override
protected void onPreExecute() {
super.onPreExecute();
myList = new ArrayList<MyObject>();
}
#Override
protected Void doInBackground(Void... params) {
while (downloadDataFromInternet)
myList.add(new MyObject(par1, par2, par3, par4));
}
#Override
protected void onPostExecute(Void result) {
if (myList.size() > 0)
adapter = new MyAdapter(this, myList);
}
}
}
Now I would like to execute this service every 10 minutes (for example) also when activity is in background, but I want that when activity come back to foreground, listView of MyFragment uses the adapter declared in the service.
Can you please help me? I have no idea about how to do this.

If you have to exactly execute the service in 10 minutes of interval .
You can use thread scheduler as :
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
//schedule a thread
scheduler.schedule(new Runnable() {
public void run() {
startservice();
}
}, 10, TimeUnit.MINUTES);
return Service.START_STICKY;
}
private startService(){
// you background service related work
while (downloadDataFromInternet)
myList.add(new MyObject(par1, par2, par3, par4));
// schedule a new thread
scheduler.schedule(new Runnable() {
public void run() {
startservice();
}
}, 10, TimeUnit.MINUTES);
}

My suggestion use database:
Once this Background task runs then update your data into database. Once you open your UI then you can read value from database and you can also update UI.

Sounds like you want to communicate between the Service and the Activity. While service is like independent from a specific context, activity isn't.
Therefore Service cannot (should not) talk to the activity directly, especially if the activity is killed/re-created.
What you should do is develop a method of intercommunicating by using a database or SharedPreferences, to store your data from within the service. Then, the activity would fetch this data and use it.

You can use Timer Task to execute some task after a specific time period
1 You need to create Subclass of TimerTask
eg
class Mytask extends Timertask{
#Override
public void run() {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
//your code you want to run in UI thread
});
}
}
2 Start this task from your service
Timer timer = new Timer();
MyTask task= new MyTask(getApplicationContext());
timer.scheduleAtFixedRate(task,1,1000*60*10); // for 10 min
Hope this is what you want

Related

Android create handler inside thread into Service

i'm writing simple android service and i want to use such as Toast or Notification but i get this error:
FATAL EXCEPTION: Thread-17116
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
i can not use runOnUiThread . my service does not know that. for example i try to use that with : this, getBaseContect(), getApplication, mContext for .runOnUiThread(new Runnable() {}
i get problem and i can not resolve problem.
this is my code:
public class TsmsService extends Service {
private Timer smsThread;
private DatabaseHandler db;
private SQLiteDatabase dbHelper;
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
smsThread = new Timer();
GetSMSThread getSMSThread = new GetSMSThread(getBaseContext());
smsThread.scheduleAtFixedRate(getSMSThread, 0, 1000); //(timertask,delay,period)
return super.onStartCommand(intent, flags, startId);
}
public class GetSMSThread extends TimerTask {
private Context mContext;
public GetSMSThread(Context context) {
mContext = context;
}
#Override
public void run() {
this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplication() , "Service is Running ... " , Toast.LENGTH_SHORT).show();
}
});
}
}
}
Try creating a Handler in onStartCommand (so, from the UI thread). Then use that Handler to trigger the Toast. For example:
private Handler mToastHandler = null;
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
...
mToastHandler = new Handler();
...
}
...
// from inside your child thread
mToastHandler.post(new Runnable() {
#Override
public void run() {
Toast.makeText(...);
}
});
Here you can use ResultReceiver that is used for the purpose of receiving a callback result from someone. In your case its Service
You can check the complete example of Service with TimerTask using ResultReceiver for updating the UI inside Activity

How to stop a service after it has finished its job?

I have a service which I start from my Activity.
Now the serivce performs some task by starting a new thread from onStartCommand()
I want to stop the service after the thread has finished its job.
I tried using a Handler like this
public class MainService extends Service{
private Timer myTimer;
private MyHandler mHandler;
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
mHandler = new MyHandler();
myTimer = new Timer();
myTimer.schedule(new MyTask(), 120000);
return 0;
}
private class MyTask extends TimerTask{
#Override
public void run() {
Intent intent = new Intent(MainService.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
mHandler.sendEmptyMessage(0);
}
}
private static class MyHandler extends Handler{
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Log.e("", "INSIDE handleMEssage");
//stopSelf();
}
}
First it was giving me a warning that if handler class is not static it will cause leaks
After I made it static, stopSelf() can not be called, because its non static.
Is my approach correct or is there a simpler way around ?
you should use IntentService rather service. It starts automatically in separate thread and stop itself as task completes.
public class MyService extends IntentService {
public MyService(String name) {
super("");
}
#Override
protected void onHandleIntent(Intent arg0) {
// write your task here no need to create separate thread. And no need to stop.
}
}
Use IntentService its base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.
try this,
private static class MyHandler extends Handler{
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Log.e("", "INSIDE handleMEssage");
MainService.this.stopSelf();;
}
}

android run service in background from time to time when the application is running

I am building an android app and when it is running I need to make a call to the my web server at each minute if the user is connected to a certain network.
I plan to use a service to make that call but how do I call it at each minute?.
I think i need to use alaarm manager but where do I initalize it? in my start activity? I only need to execute the service when my app is running.
Thanks for your help.
If you want to call server only if app is running then no need to use alarm manager. there are other options like
CoundDownTimer
Thread
I prefer CoundownTimer in that scenario and you can use like this
CountDownTimer countDownTimer = new CountDownTimer(1000000, 60 * 1000) {
#Override
public void onTick(long millisUntilFinished) {
// Do something on a tick.
}
#Override
public void onFinish() {
// Do something, maybe?
this.start();
}
};
countDownTimer.start();
Try this ::
You can call this timer in your activity where you want it
private Timer autoUpdate;
#Override
public void onResume() {
super.onResume();
autoUpdate = new Timer();
autoUpdate.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
//call your service from here
}
});
}
}, 0, 60000);//set time interval according to your requirement
}
Feel free to ask if you have any query :)
in Activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TestAlarmMessageReceiver almesr = new TestAlarmMessageReceiver(this, time);
}
BroadcastReceiver:
public class TestAlarmMessageReceiver extends BroadcastReceiver {
public TestAlarmMessageReceiver() {
}
public TestAlarmMessageReceiver(Context context, int timeout) { //timeout in seconds
AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, TestAlarmMessageReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(),
timeout*60*1000, pendingIntent);
}
#Override
public void onReceive(Context arg0, Intent arg1) {
arg0.startService(new Intent(arg0, TestMessageService.class));
}
}
Service:
public class TestMessageService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
messageUpdateTask();
return super.onStartCommand(intent, flags, startId);
}
private void messageUpdateTask(){
GetMessagesUpdateAsyncTak getMessUpd = new GetMessagesUpdateAsyncTak();
getMessUpd.execute(this);
}
}
Call your web server using AsynTask http://developer.android.com/reference/android/os/AsyncTask.html
In onPost method in AsynTask class wait for one minute and call AsynTask.
You can call AsynTask by using below code :
BLSyncingProcedure objSyncingProcedure=new BLSyncingProcedure();
objSyncingProcedure.execute(HomeScreen.this);
BLSyncingProcedure is your AsynTask class name.

Only one instance of Thread in service

I initialize new Thread in service but when i start service the new one is made and it make my app crash beacause I use camera in it.
How to make that it will be ony one instance of that Thread?
When Thread is closing? If I close service where I made it, it will be also closed?
you could use a lock or a static variable:
private static boolean isThreadRunning;
and then in your service:
if(isThreadRunning)
return;
Thread t=new Thread(new Runnable(){
protected void run(){
isThreadRunning=true;
while(yourcondition){
//your thread code...
}
isThreadRunning=false;
//if you want to start another thread after this one is ended, you should post a message to a handler here and it should start another thread like this
}
});
You can also achieve this using Handler class, which is recommended by Google in thread operations. The code bellow shows generic example how to use it in Service.
public class MyService extends Service{
private final Handler handler = new Handler();
private final static int RUNABLE_WHAT=6558057;
private final static int PEROID=6*1000;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(!handler.hasMessages(RUNABLE_WHAT))
{
handler.sendMessageDelayed(new Worker().extractMessage(), PEROID);
}
return START_STICKY;
}
private class Worker implements Runnable{
#Override
public void run() {
//DO WORK HERE
handler.sendMessageDelayed(new Worker().extractMessage(), PEROID);
}
private Message extractMessage()
{
Message message = Message.obtain(handler, this);
message.what=RUNABLE_WHAT;
return message;
}
}
}

Android - how to run a task via "handler" periodically within a service-intent (worker-thread)

My question is Android related:
How do I run a task every 20 seconds within an intentservice ?
Problem is, I have to init some classes which will be used in the Handler "run" process.
It works one time - but then the service stops & the application crashes when the handler-loop starts again after 20 seconds (probably because of the classes that got eliminated when the service stopped?). So maybe the solution is to get the service to stay running as long as the Handler runs or to throw away the code and do it right ?
Hope, someone can help me.
public class Fadenzieher extends IntentService{
private Handler handler = new Handler();
private Runnable timedTask = new Runnable(){
#Override
public void run() {
// My functions get called here...
// class1member.getDBWorkdone();
handler.postDelayed(timedTask, 20000);
handler.obtainMessage();
}};
public Fadenzieher() {
super("Fadenzieher");
}
#Override
protected void onHandleIntent(Intent intent) {
// SOME INITIALISING
// I have to init some vars & functions here that
// will also be used inside the handler loop
// Class1 class1member = new Class1();
// class1member.startUpDB();
handler.post(timedTask); }
Thank you very much in advance!!!
---- So this is the updated code now (14. nov. 2011)
public class Fadenzieher extends Service{
private static final long UPDATE_INTERVAL = 60000;
Context context = this;
private Timer timer = new Timer();
DbHelper dbHelper;
public void onCreate(){
dbHelper = new DbHelper(context);
runTheLoop();
}
protected void runTheLoop() {
timer.scheduleAtFixedRate(new TimerTask(){
#Override
public void run() {
dbHelper.dosomethings();
Toast.makeText(context, "CALL", Toast.LENGTH_LONG).show();
}}, 0, UPDATE_INTERVAL);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Starte Service“, Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent,flags,startId);
}
public void onDestroy() {
super.onDestroy();
dbHelper.close();
Toast.makeText(this, "Stoppe Service“, Toast.LENGTH_LONG).show();
}
// We return the binder class upon a call of bindService
#Override
public IBinder onBind(Intent arg0) {
return mBinder;
}
public class MyBinder extends Binder {
Fadenzieher getService() {
return Fadenzieher.this;
}
}
}
The whole application crashes immediately.
How do I run a task every 20 seconds within an intentservice ?
That is not an appropriate use of IntentService. Use a regular Service, please.
It works one time - but then the service stops & the application crashes when the handler-loop starts again after 20 seconds
IntentService shuts down when onHandleIntent() returns, which is why this is breaking for you. Use a regular Service, please.
Also:
Please allow the user to configure the polling period
Make sure that this service will shut down when the user no longer wants it to be running

Categories

Resources