Page 68 - 24JAM_Pintar_Pemrograman_Android__1-libre
P. 68
24 JAM!! Pintar Pemrograman Android
1: <?xml version="1.0" encoding="utf-8"?>
2: <resources>
3: <string name="hello">ServiceSederhana!</string>
4: <string name="app_name">Service Background
5: Sederhana</string>
6: <string name="startBtn">Start</string>
7: <string name="stopBtn">Stop</string>
8: </resources>
9:
3. Kita bikin layoutnya di main.xml
1: <?xml version="1.0" encoding="utf-8"?>
2: <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
3: android:layout_width="fill_parent"
4: android:layout_height="fill_parent"
5: android:orientation="vertical" >
6:
7: <TextView
8: android:layout_width="fill_parent"
9: android:layout_height="wrap_content"
10: android:text="@string/hello"
11: android:textSize="24dip" />
12:
13: <Button
14: android:id="@+id/startBtn"
15: android:layout_width="match_parent"
16: android:layout_height="wrap_content"
17: android:text="@string/startBtn" >
18: </Button>
19:
20: <Button
21: android:id="@+id/stopBtn"
22: android:layout_width="match_parent"
23: android:layout_height="wrap_content"
24: android:text="@string/stopBtn" >
25: </Button>
26:
27: </LinearLayout>
4. Buat folder raw didalam folder res. Taruh saja file
mp3nya di folder raw.
5. Membuat class MyService.java
1: package com.serv.bg;
2:
3: import android.app.Service;
4: import android.content.Intent;
5: import android.media.MediaPlayer;
6: import android.os.IBinder;
7:
8: public class MyService extends Service{
9: MediaPlayer mp;
10:
11: @Override
12: public IBinder onBind(Intent arg0) {
13: // TODO Auto-generated method stub
14: return null;
15: }
16: @Override
17: public void onCreate(){
18: mp=MediaPlayer.create(this, R.raw.beraksi);
19: mp.setLooping(false);
20: }
21:
Dapatkan materi terbaru di 68
www.omayib.com