android的service学习案例------自己做的音乐播放器,让service后台播放mp3文件(是burning.mp3哦!)...

当需要创建在后台运行的程序的时候,就要使用到Service。Service 可以分为有无限生命和有限生命两种。特别需要注意的是Service跟Activities是不同的(简单来说可以理解为后台与前台的区别),例如,如果 需要使用Service的话,需要调用startService(),从而利用startService()去调用Service中的 OnCreate()和onStart()方法来启动一个后台的Service。

启动一个Service的过程如 下:context.startService()<wbr>-&gt;onCreate()- &gt;onStart()-&gt;Service running</wbr>其中onCreate()可以进行一些服务的初始化工作,onStart()则启动服务。
  停止一个Service的过程如下:context.stopService() | ->onDestroy() ->Service stop
  接下来的实例是一个利用后台服务播放音乐的小例子,点击start运行服务,点击stop停止服务。

MainActivity的源代码:

package com.example.test1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
private Button playButton = null;
private Button stopButton = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playButton = (Button)findViewById(R.id.startPlayButton);
stopButton = (Button)findViewById(R.id.stopPlayButton);
playButton.setOnClickListener(new ButtonListen());
stopButton.setOnClickListener(new ButtonListen());
}

class ButtonListen implements OnClickListener{

public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.startPlayButton:
startService(new Intent(MainActivity.this, MyService.class));
break;
case R.id.stopPlayButton:
stopService(new Intent(MainActivity.this, MyService.class));
break;
default:
break;
}
}

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

MyService的源代码:

package com.example.test1;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {
MediaPlayer player = null;
public MyService() {
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(this,
"MyService Createed!",
Toast.LENGTH_SHORT).show();
player = MediaPlayer.create(this, R.raw.burning);
player.setLooping(false);
}

@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
Toast.makeText(this,
"MyService onStart!",
Toast.LENGTH_SHORT).show();
player.start();
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
player.stop();
Toast.makeText(this,
"MyService onDestroy",
Toast.LENGTH_SHORT).show();
}

}

源码稍后提供下载:http://download.csdn.net/detail/yanzi1225627/4463948

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值