Note that there are some explanatory texts on larger screens.

plurals
  1. POActivity closes after service stopped in android
    primarykey
    data
    text
    <p>Activity closes when service is stopped The code which i am using to start service is</p> <pre><code>Intent intent2 = new Intent(getApplicationContext(), TService.class); startService(intent2); </code></pre> <p>and the code to stop service is </p> <pre><code> Intent intent2 = new Intent(getApplicationContext(), TService.class); stopService(intent2); </code></pre> <p>i tried an alternative to use bind service but it showed the same behavior by closing the activity </p> <p>code to start bind service was </p> <pre><code>Intent intent2 = new Intent(getApplicationContext(), TService.class); bindService(intent2, mServerConn, Context.BIND_AUTO_CREATE); startService(intent2); </code></pre> <p>&amp; code to stop bind service was </p> <pre><code>stopService(new Intent(getApplicationContext(), TService.class)); unbindService(mServerConn); </code></pre> <p>My service TService.class</p> <pre><code>public class TService extends Service { @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } @Override public void onDestroy() { Log.d("service", "destroy"); super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("TAG", "service started"); context = getApplicationContext(); return START_NOT_STICKY; } } </code></pre> <p>My Activity MyActivity.java</p> <pre><code>public class MyActivity extends Activity{ Button start,stop; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); start=(Button) findViewById(R.id.start); stop=(Button) findViewById(R.id.stop); final ServiceConnection mServerConn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { Log.d("LOG_TAG", "onServiceDisconnected"); } @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub Log.d("LOG_TAG", "onServiceConnected"); } }; start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent2 = new Intent(getApplicationContext(), TService.class); intent2.putExtra("value", "1"); startService(intent2); } }); stop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent2 = new Intent(getApplicationContext(), TService.class); intent2.putExtra("value", "2"); stopService(intent2); } }); } private int getClicks() { clickCount++; return clickCount % 2 == 0 ? R.drawable.start : R.drawable.stop; } private boolean isMyServiceRunning() { ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { if (TService.class.getName().equals(service.service.getClassName())) { return true; } } return false; } @Override protected void onPause() { } } </code></pre> <p>I am really stuck on it ,any suggestion or solution will be accepted Thanks in advance</p>
    singulars
    1. This table or related slice is empty.
    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