Note that there are some explanatory texts on larger screens.

plurals
  1. POMy service stop after the back button
    primarykey
    data
    text
    <p>I have a service and an activity that communicate. When I click the button(I have galaxy s3 there is only one button) then my activity of course disapear and my service is keep running but if I click the back (touch) button then my service is destroyed. How can I change that?I want the service to keep running untill the activity destroys it.</p> <p><strong>EDIT</strong></p> <p>Here is the code:</p> <p>Service: public class MyService extends Service { private static final String TAG = "BroadcastService"; public static final String BROADCAST_ACTION = "com.websmithing.broadcasttest.displayevent"; private final Handler handler = new Handler(); private Intent intent; int counter = 0;</p> <pre><code> @Override public void onCreate() { super.onCreate(); intent = new Intent(BROADCAST_ACTION); } @Override public void onStart(Intent intent, int startId) { // handler.removeCallbacks(sendUpdatesToUI); handler.postDelayed(sendUpdatesToUI, 1000); // 1 second } private Runnable sendUpdatesToUI = new Runnable() { public void run() { DisplayLoggingInfo(); handler.postDelayed(this, 1000); // 10 seconds } }; private void DisplayLoggingInfo() { intent.putExtra("counter", String.valueOf(++counter)); sendBroadcast(intent); } @Override public IBinder onBind(Intent intent) { return null; } @Override public void onDestroy() { handler.removeCallbacks(sendUpdatesToUI); super.onDestroy(); } } </code></pre> <p>Activity:</p> <pre><code>public class MainActivity extends Activity { private static final String TAG = "BroadcastTest"; private Intent intent; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intent = new Intent(this, MyService.class); startService(intent); registerReceiver(broadcastReceiver, new IntentFilter(MyService.BROADCAST_ACTION)); } private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { updateUI(intent); } }; @Override public void onDestroy() { super.onPause(); unregisterReceiver(broadcastReceiver); stopService(intent); } private void updateUI(Intent intent) { String counter = intent.getStringExtra("counter"); Log.d(TAG, counter); TextView txtCounter = (TextView) findViewById(R.id.textView1); txtCounter.setText(counter); } } </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.
    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