1、Activity通过startService
参考:http://android.tgbus.com/Android/tutorial/201107/359322.shtml
启动一个Service的过程如下:context.startService() ->onCreate()- >onStart()->Service running其中onCreate()package test.cbl.srv;
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) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service created", Toast.LENGTH_LONG).show();
Log.i(TAG, "onCreate");
player = MediaPlayer.create(this,R.raw.mine);
player.setLooping(false);
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stoped", Toast.LENGTH_LONG).show();
Log.i(TAG, "onDestroy");
player.stop();
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Start", Toast.LENGTH_LONG).show();
Log.i(TAG, "onStart");
player.start();
}
}
1.2 Activity中启动Service
package test.cbl.srv;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ServiceDemo extends Activity implements OnClickListener {
private static final String TAG = "ServiceDemo";
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.i(TAG, "onClick: starting service");
startService(new Intent(this, MyService.class));
break;
case R.id.buttonStop:
Log.i(TAG, "onClick: stopping service");
stopService(new Intent(this, MyService.class));
break;
}
}
}
1.3 string.xml字符串定义
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">播放器服务</string>
<string name="start">Start</string>
<string name="stop">Stop</string>
<string name="services_demo">Service Demo</string>
</resources>
1.4 main.xml布局文件layout
<?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"
android:gravity="center">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/services_demo" android:gravity="center" android:textSize="20sp" android:padding="20dp"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/buttonStart" android:text="@string/start"></Button>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/stop" android:id="@+id/buttonStop"></Button>
</LinearLayout>
1.5 Manifest.xml 添加Service声明
<service android:enabled="true" android:name=".MyService"/>
2、bindService方式启动Service
2.1 Activity中Oncreate中:
//建立和service的通讯
Intent bindIntent = new Intent(MainActivity.this, CounterService.class);
bindService(bindIntent, serviceConnection, Context.BIND_AUTO_CREATE);
2.2 类中创建connection对象
private ServiceConnection serviceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
counterService = ((CounterService.CounterBinder)service).getService();
Log.i(LOG_TAG, "Counter Service Connected");
}
public void onServiceDisconnected(ComponentName className) {
counterService = null;
Log.i(LOG_TAG, "Counter Service Disconnected");
}
};