标签:
(三)、Service的生命周期:
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/button_main_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="播放音乐"/>
<Button
android:id="@+id/button_main_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="暂停音乐"/>
<Button
android:id="@+id/button_main_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="停止音乐"/>
<Button
android:id="@+id/button_main_exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="关闭当前窗体"/>
<Button
android:id="@+id/button_main_stopservice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="停止服务"/>
</LinearLayout>
publicclass MainActivity extends Activity {
privatestaticfinal String TAG = "MainActivity";
private Intent intent = null;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
Log.i(TAG, "==onCreate执行");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(this, StartServicePlayMusic.class);
}
@Override
protectedvoid onDestroy() {
Log.i("MainActivty", "==onDestroy()");
super.onDestroy();
if (intent != null) {
// 停止服务可以通过stopService(intent)来停止,也可以intent到Service程序中,通过stopSelf()来停止。
stopService(intent);
}
}
publicvoid clickButton(View view) {
int type = 0;
switch (view.getId()) {
case R.id.button_main_play:
type = 1;
break;
case R.id.button_main_pause:
type = 2;
break;
case R.id.button_main_stop:
type = 3;
break;
case R.id.button_main_exit:
finish();
break;
case R.id.button_main_stopservice:
// 停止服务可以通过stopService(intent)来停止,也可以intent到Service程序中,通过stopSelf()来停止
// stopService(intent);
// finish();
type = 4;
break;
}
Bundle bundle = new Bundle();
bundle.putInt("type", type);
intent.putExtras(bundle);
startService(intent);
}
}
publicclass StartServicePlayMusic extends Service {
privatestaticfinal String TAG = "StartServicePlayMusic";
private MediaPlayer mediaPlayer;
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "==onBind执行");
returnnull;
}
@Override
publicvoid onCreate() {
Log.i(TAG, "==onCreate执行");
super.onCreate();
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(this, R.raw.hitta);
mediaPlayer.setLooping(false);
}
}
@Override
publicint onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "==onStartCommand执行");
if (intent != null) {
Bundle bundle = intent.getExtras();
int type = bundle.getInt("type");
switch (type) {
case 1:
play();
break;
case 2:
pause();
break;
case 3:
stop();
break;
case 4:
stopSelf();
break;
}
}
returnSTART_STICKY;
}
@Override
publicvoid onDestroy() {
Log.i(TAG, "==onDestroy执行");
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
publicvoid play() {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
}
publicvoid pause() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
}
publicvoid stop() {
if (mediaPlayer != null) {
mediaPlayer.stop();
}
}
}
<service
android:name=".StartServicePlayMusic">
<intent-filter>
<actionandroid:name="com.steven.startservice.playmusic"/>
</intent-filter>
</service>
publicclass DownloadService extends IntentService {
privatestaticfinal String TAG = "DownloadService";
private String urlString = "https://www.google.com.hk/images/srpr/logo11w.png";
private NotificationCompat.Builder builder = null;
private NotificationManager manager = null;
public DownloadService() {
super("");
}
@Override
protectedvoid onHandleIntent(Intent intent) {
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
builder = new NotificationCompat.Builder(getApplicationContext());
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle("提示:");
builder.setContentText("图片加载完成,请点击查看!");
builder.setTicker("图片加载完成");
builder.setAutoCancel(true);
Intent intent2 = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pIntent = PendingIntent.getActivity(
getApplicationContext(), 0, intent2,
PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(pIntent);
byte[] data = HttpClientHelper.loadByteFromURL(urlString);
boolean flag = SDCardHelper.saveFileToSDCard(data, "Download",
"logo11w.png");
if (flag) {
manager.notify(1, builder.build());
}
}
}
publicclass DownloadService extends Service {
privatestaticfinal String TAG = "DownloadService";
private String urlString = "https://www.google.com.hk/images/srpr/logo11w.png";
private NotificationCompat.Builder builder = null;
private NotificationManager manager = null;
@Override
public IBinder onBind(Intent intent) {
returnnull;
}
@Override
publicvoid onCreate() {
super.onCreate();
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
builder = new NotificationCompat.Builder(getApplicationContext());
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle("提示:");
builder.setContentText("图片加载完成,请点击查看!");
builder.setTicker("图片加载完成");
builder.setAutoCancel(true);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pIntent = PendingIntent
.getActivity(getApplicationContext(), 0, intent,
PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(pIntent);
}
@Override
publicint onStartCommand(Intent intent, int flags, int startId) {
new Thread(new Runnable() {
@Override
publicvoid run() {
byte[] data = HttpClientHelper.loadByteFromURL(urlString);
boolean flag = SDCardHelper.saveFileToSDCard(data, "Download",
"logo11w.png");
if (flag) {
manager.notify(1, builder.build());
stopSelf();
}
}
}).start();
returnSTART_STICKY;
}
@Override
publicvoid onDestroy() {
super.onDestroy();
}
}
publicclass MainActivity extends Activity {
privatestaticfinal String TAG = "MainActivity";
private Intent intent;
private ServiceConnection conn = null;
private BindServicePlayMusic musicService;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
Log.i(TAG, "==onCreate执行");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 启动服务有多种写法:
// 可以通过new Intent(action字符串)来实现;
// intent = new Intent("com.steven.bindservice.playmusic");
intent = new Intent(this, BindServicePlayMusic.class);
conn = new ServiceConnection() {
@Override
publicvoid onServiceDisconnected(ComponentName name) {
musicService = null;
}
@Override
publicvoid onServiceConnected(ComponentName name, IBinder service) {
musicService = ((BindServicePlayMusic.MyBinder) service)
.getService();
if (musicService != null) {
musicService.play();
}
}
};
}
publicvoid clickButton(View view) {
switch (view.getId()) {
case R.id.button_main_play:
if (musicService == null) {
bindService(intent, conn, Context.BIND_AUTO_CREATE);
} else {
musicService.play();
}
break;
case R.id.button_main_pause:
if (musicService != null) {
musicService.pause();
}
break;
case R.id.button_main_stop:
if (musicService != null) {
musicService.stop();
}
break;
case R.id.button_main_exit:
finish();
break;
case R.id.button_main_stopservice:
// BindService中stopService(intent)不起作用,要通过undbindService来停止服务
// stopService(intent);
// musicService = null的目的是如果停止服务后,再次”播放“,可以正常执行。
// 如果不将musicService设置为null,再次播放时,将直接执行musicService.play(),而不执行bindService(),那么会导致异常
musicService = null;
unbindService(conn);
break;
}
}
}
publicclass BindServicePlayMusic extends Service {
privatestaticfinal String TAG = "BindServicePlayMusic";
private MediaPlayer mediaPlayer;
private IBinder binder = null;
@Override
publicvoid onCreate() {
Log.i(TAG, "==onCreate执行");
super.onCreate();
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(this, R.raw.heavencity);
mediaPlayer.setLooping(false);
}
binder = new MyBinder();
}
class MyBinder extends Binder {
public BindServicePlayMusic getService() {
return BindServicePlayMusic.this;
}
};
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "==onBind执行");
play();
returnbinder;
}
@Override
publicboolean onUnbind(Intent intent) {
Log.i(TAG, "==onUnbind执行");
returnsuper.onUnbind(intent);
}
@Override
publicvoid onDestroy() {
Log.i(TAG, "==onDestroy执行");
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
publicvoid play() {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
}
publicvoid pause() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
}
publicvoid stop() {
if (mediaPlayer != null) {
mediaPlayer.stop();
}
}
}
<service
android:name=".BindServicePlayMusic">
<intent-filter>
<actionandroid:name=“com.steven.bindservice.playmusic"/>
</intent-filter>
</service>
标签:
原文地址:http://blog.csdn.net/xdf0101/article/details/51918459