/ Published in: Java
Expand |
Embed | Plain Text
//MyService.java package com.v3; 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 { MediaPlayer player; @Override public void onCreate() { Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show(); Log.d(TAG, "onCreate"); Log.i(TAG, "onCreate"); player = MediaPlayer.create(this, R.raw.airtel); player.setLooping(false); // Set looping } @Override public void onStart(Intent intent, int startid) { Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); Log.d(TAG, "onStart"); player.start(); } @Override public IBinder onBind(Intent intent) { return null; } @Override public void onDestroy() { Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); Log.d(TAG, "onDestroy"); player.stop(); } } //PlayingActivity.java package com.v3; 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 PlayingActivity extends Activity { Button buttonStart, buttonStop; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.d(TAG, "onClick: starting srvice"); startService(new Intent(PlayingActivity.this, MyService.class)); } }); Log.d(TAG, "onClick: stopping srvice"); stopService(new Intent(PlayingActivity.this, MyService.class)); } }); } } //menifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.v3" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".PlayingActivity" 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=".MyService" /> </application> </manifest> //main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:text="PLAY" android:id="@+id/btnStop" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="STOP" android:id="@+id/btnPlay" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout>
You need to login to post a comment.
