Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>ServiceDemo.java:</p> <pre><code>public class ServicesDemo extends Activity implements OnClickListener { private static final String TAG = "ServicesDemo"; Button buttonStart, buttonStop; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); buttonStart = (Button) findViewById(R.id.buttonStart); buttonStop = (Button) findViewById(R.id.buttonStop); buttonStart.setOnClickListener(this); buttonStop.setOnClickListener(this); } public void onClick(View src) { switch (src.getId()) { case R.id.buttonStart: Log.w(TAG, "onClick: starting srvice"); startService(new Intent(this, MyService.class)); startActivity(new Intent(getApplicationContext(),Second.class)); break; case R.id.buttonStop: Log.w(TAG, "onClick: stopping srvice"); stopService(new Intent(this, MyService.class)); break; } } } </code></pre> <p>MyService.java:</p> <pre><code>package com.example; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; import android.util.Log; import android.widget.Toast; public class MyService extends Service { private static final String TAG = "MyService"; MediaPlayer player; @Override public IBinder onBind(Intent intent) { Log.w(" ibinder ",""); return null; } @Override public void onCreate() { Toast.makeText(this, "My Service Created",0).show(); Log.w(TAG, "onCreate"); player = MediaPlayer.create(this,R.raw.frm7v1); player.setLooping(true); // Set looping } @Override public void onDestroy() { Toast.makeText(this, "My Service Stopped",0).show(); Log.w(TAG, "onDestroy"); player.stop(); } @Override public void onStart(Intent intent, int startid) { Toast.makeText(this, "My Service Started :"+intent+" start id :"+startid,0).show(); Log.d(TAG, "onStart"); player.start(); } } </code></pre> <p>Declare the following attribute in manifest file:</p> <pre><code> &lt;service android:enabled="true" android:name=".MyService" /&gt; </code></pre>
 

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