Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to stop service in background with toggle button in Android?
    primarykey
    data
    text
    <p>I'm implementing running the service in background and service will be stop and start click on toggle button.Service is start in 1st activity and stop the service in another activity when click on toggle button.When i run the application service is automatically start in 1st activity which i start in onCreate() method in 1st activity and another activity toggle button status is already on status but when i toggle button is going to off service stop but when i back to 1st activity service is again start.Please can any one help me.Here is my code </p> <pre><code>public class MyService extends Service { private static final String TAG = "MyService"; MediaPlayer player; private final String StrMyService="myservice"; @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show(); Log.d(TAG, "onCreate"); player = MediaPlayer.create(this, R.raw.braincandy); player.setLooping(false); // Set looping } @Override public void onDestroy() { Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); Log.d(TAG, "onDestroy"); player.stop(); } @Override public void onStart(Intent intent, int startid) { Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); Log.d(TAG, "onStart"); player.start(); } } public class Service_Demo extends Activity implements OnClickListener { private static final String TAG = "ServicesDemo"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); System.out.println("In OnCreate("); startService(new Intent(this, MyService.class)); } } public class Toggle_Activity extends Activity { ToggleButton tgButton; private boolean isService=false; private String strService; public final String service_Prefs="servicePrefs"; private static final String StrMyService = "zdf"; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.toggle); final SharedPreferences servicePrefs=this.getSharedPreferences("Service_Prefs",MODE_WORLD_READABLE); strService=servicePrefs.getString(StrMyService , "myservice"); Log.e("",""+strService); final boolean mBool = servicePrefs.getBoolean("myservice", true); Log.e("Boolean Value mBool","="+mBool); Boolean b = mBool; Log.e("Update pref", b.toString()); tgButton = (ToggleButton)findViewById(R.id.toggleButton); tgButton.setChecked(mBool); final boolean mBool1 = servicePrefs.getBoolean("myservice", false); final Boolean c = mBool1; Log.e("Update pref", c.toString()); tgButton=(ToggleButton)findViewById(R.id.toggleButton); tgButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(tgButton.isChecked()) { startService(new Intent(Toggle_Activity.this , MyService.class)); System.out.println("Service is started in togglr button"); } else { stopService(new Intent(Toggle_Activity.this,MyService.class)); System.out.println("Service is stopped in togglr button"); } } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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