Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>MrJre is correct that onStart is depreciated and that you should be using onStartCommand().</p> <p>If you want to get this to work, there is a better way. </p> <p>I am doing something similar, as in wanting to update a UI from results happening in a service. This was not particularly easy. (In my opinion)</p> <p>Here's how to do it: (First off scrap your existing code)</p> <p>In UI class add: </p> <pre><code>public Intent service; service = new Intent(thisContext, TimerService.class); service.putExtra("ms", ms); startService(service); //bind service to the UI **Important** bindService(); IntentFilter timerFilter = new IntentFilter("TimerIntent"); // Filter that gets stuff from the service registerReceiver(myReceiver, timerFilter); void bindService() { Intent newIntent = new Intent(this, TimerService.class); bindService(newIntent, mConnection, Context.BIND_AUTO_CREATE); mIsBound = true; } private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName className, IBinder binder) { s = ((TimerService.MyBinder) binder).getService(); } @Override public void onServiceDisconnected(ComponentName className) { s = null; } }; public void releaseBind() { if (mIsBound) { unbindService(mConnection); mIsBound = false; } } // Now in this class we need to add in the listener that will update the UI (the receiver registered above) private BroadcastReceiver myReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub //Get Bundles Bundle extras = intent.getExtras(); /* DO ANY UI UPDATING YOU WANT HERE (set text boxes, etc.) TAKING INFO FROM THE "extras" Bundle ie: setting the clock*/ //ie: int timerTest = extras.getInt("0"); // Now update screen with value from timerTest } }; </code></pre> <p>Service File: </p> <pre><code>public class TimerService extends Service { public TimerService () { super(); } private final IBinder mBinder = new MyBinder(); public Timer clockTimer = new Timer(); public int timer = 0; // We return the binder class upon a call of bindService @Override public IBinder onBind(Intent arg0) { return mBinder; } @Override public int onStartCommand(Intent intent, int flags, int startId) { // After service starts this executes Bundle extras; extras = intent.getExtras(); /* Call a function to do stuff here. Like if you are a clock call a timer function updates every second */ // Here's an example, modify to fit your needs. clock(); return START_STICKY; } public class MyBinder extends Binder { TimerService getService() { return TimerService.this; } } public void clock() { clockTimer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { try { // Some function ie: Time = Time + 1 // /* MAKE SURE YOU BROADCAST THE RECEIVER HERE. This is what you send back to the UI. IE:*/ timer = timer+ 1; // increment counter Intent intent = new //Bundle the timervalue with Intent intent.putExtra("0", timer); intent.setAction("TimerIntent"); sendBroadcast(intent); // finally broadcast to the UI } catch(Exception ie) { } } }, 0, // Delay to start timer 1000); // how often this loop iterates in ms (so look runs every second) } </code></pre> <p>There might be some syntax errors in this code as I've just modified my existing and working code to try and fit your needs. There will obviously need to also be some modifications depending on what you want to do. But follow this framework and you will be able to do what you are trying to do. </p> <p>This works for me, so hopefully you can modify this to work for you. (Only thing I've left out are the imports, but you should be able to easily figure that out) </p> <p>Key points: </p> <ul> <li>Bind service to UI </li> <li>Register listener in UI file to respond to the broadcast from inside the service.</li> </ul> <p>Cheers. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload