Hello. I am trying to implement my own service, which produces a process in the background. In particular, I tried to launch an example from a book that implements background playback of an audio file. Of course, at the start (the button is pressed, the service is started in the click listener code) the application in the emulator crashes with the standard message
has stopped unexpectedly. please try again (Force close) [closed]
I would be glad if someone would give the simplest example with a similar task, with explanations. Example code that falls.
Application Manifest (everything is in order)
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.samples.servicelaunch" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="3" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".LaunchActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:enabled="true" android:name=".PlayService" > </service> </application> </manifest>
Activity class code that starts the PlayService service:
package com.samples.servicelaunch; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class LaunchActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Button btnStart=(Button)findViewById(R.id.btn_start); final Button btnStop=(Button)findViewById(R.id.btn_stop); btnStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub startService(new Intent(LaunchActivity.this,PlayService.class)); Log.v(this.getClass().getName(), "Service started."); } }); btnStop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.v(this.getClass().getName(), "Service stopped."); stopService(new Intent(LaunchActivity.this,PlayService.class)); } }); } }
Here the btnStart button should start the service according to the idea (create or start if it has already been created).
And finally, the code of the class that inherits the service (Service):
package com.samples.servicelaunch; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; import android.widget.Toast; public class PlayService extends Service { MediaPlayer mPlayer; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { Toast.makeText(this, "Service Created", Toast.LENGTH_SHORT).show(); mPlayer = MediaPlayer.create(this, R.raw.sample); mPlayer.setLooping(false); } @Override public void onDestroy() { Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show(); mPlayer.stop(); } @Override public void onStart(Intent intent, int startid) { Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show(); mPlayer.start(); } }