MainActivity.Java
1 package com.example.test; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.util.Log; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 11 public class MainActivity extends Activity implements OnClickListener{ 12 13 private static final String TAG = "ServicesDemo"; 14 Button buttonStart, buttonStop; 15 16 @Override 17 public void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 21 buttonStart = (Button) findViewById(R.id.buttonStart); 22 buttonStop = (Button) findViewById(R.id.buttonStop); 23 24 buttonStart.setOnClickListener(this); 25 buttonStop.setOnClickListener(this); 26 } 27 28 public void onClick(View src) { 29 switch (src.getId()) { 30 case R.id.buttonStart: 31 Log.d(TAG, "onClick: starting srvice"); 32 startService(new Intent(this, MyService.class)); 33 break; 34 case R.id.buttonStop: 35 Log.d(TAG, "onClick: stopping srvice"); 36 stopService(new Intent(this, MyService.class)); 37 break; 38 } 39 } 40 }
MyService.Java
1 package com.example.test; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.media.MediaPlayer; 6 import android.os.IBinder; 7 import android.util.Log; 8 import android.widget.Toast; 9 10 public class MyService extends Service { 11 private static final String TAG = "MyService"; 12 MediaPlayer player; 13 14 @Override 15 public IBinder onBind(Intent intent) { 16 return null; 17 } 18 19 @Override 20 public void onCreate() { 21 Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show(); 22 Log.d(TAG, "onCreate"); 23 } 24 25 @Override 26 public void onDestroy() { 27 Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); 28 Log.d(TAG, "onDestroy"); 29 } 30 31 @Override 32 public void onStart(Intent intent, int startid) { 33 Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); 34 Log.d(TAG, "onStart"); 35 } 36 }
activity_main.xml
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 <Button 12 android:id="@+id/buttonStart" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:layout_alignLeft="@+id/buttonStop" 16 android:layout_alignParentTop="true" 17 android:layout_marginTop="74dp" 18 android:text="开始" /> 19 20 <Button 21 android:id="@+id/buttonStop" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:layout_alignParentBottom="true" 25 android:layout_centerHorizontal="true" 26 android:layout_marginBottom="122dp" 27 android:text="结束" /> 28 29 </RelativeLayout>
AndroidManifest.xml,添加
1 <service android:enabled="true" android:name=".MyService" />