Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Thanks javaJoe, although your answer didn't solved my problem it gave me some a good ideas.</p> <p>What I did:</p> <ol> <li><p>in the Activity onCreate, check if my service is running, if so bind it else, create new one and bind it.</p></li> <li><p>Transferring arguments between the Service and the Activity using setters and getters.</p></li> <li><p>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.</p></li> </ol> <p>I don't know if this architecture is ideal, i'd like to get some feedback.</p> <p>Here is the code:</p> <p>Activity:</p> <pre><code> @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(); } </code></pre> <p>Broadcastreciver:</p> <pre><code>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"); } } </code></pre> <p>Service:</p> <pre><code>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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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