这次我做的是一个音乐播放器:
首先还是main.xml:
布局还是很简单,只是添加了歌曲名,歌手,还有开始,暂停和结束。
我写了个MyService的类:
package wyf.ytl;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.*;
import android.app.Service;
import android.media.MediaPlayer;
import android.os.IBinder;
public class MyService extends Service
{
MediaPlayer mp;
ServiceReceiver serviceReceiver;
int status =1;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate()
{
status=1;
serviceReceiver =new ServiceReceiver();
IntentFilter fileter=new IntentFilter();
fileter.addAction("wyf.ytl.control");
registerReceiver(serviceReceiver,fileter);
super.onCreate();
}
@Override
public void onDestroy()
{
unregisterReceiver(serviceReceiver);
super.onDestroy();
}
public class ServiceReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent)
{
int action = intent.getIntExtra("ACTION",1);
switch(action){
case 1:
if(status==1){
mp=MediaPlayer.create(context,R.raw.nx);
status=2;
Intent sendIntent=new Intent("wyf.yfl.update");
sendIntent.putExtra("update", 2);
sendBroadcast(sendIntent);
mp.start();
}
else if(status==2){
mp.pause();
status=3;
Intent sendIntent= new Intent("wyf.yfl.update");
sendIntent.putExtra("update",3);
sendBroadcast(sendIntent);
}
break;
case 2:
if(status==2||status==3){
mp.stop();
status=1;
Intent sendIntent = new Intent("wyf.ytl.update");
sendIntent.putExtra("update", 1);
sendBroadcast(sendIntent);
}
}
}
}
}
这次Service用到了广播机制,在onCreate时注册一个BroadcaseReceiver,在onDestroy时取消,在ServiceReceiver类中进行判断,接收到了数据后判断播放状态进行转换。
package wyf.ytl;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ServiceActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
Button start;
Button stop;
ActivityReceiver activityReceiver;
int status=1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start=(Button) findViewById(R.id.start);
stop=(Button)findViewById(R.id.stop);
start.setOnClickListener(this);
stop.setOnClickListener(this);
activityReceiver= new ActivityReceiver();
IntentFilter filter= new IntentFilter();
filter.addAction("wyf.ytl.update");
registerReceiver(activityReceiver,filter);
Intent intent= new Intent(this,MyService.class);
startService(intent);
}
public class ActivityReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int update=intent.getIntExtra("update",-1);
switch(update){
case 1:
status=1;
break;
case 2:
status=2;
break;
case 3:
status=3;
break;
}
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent= new Intent("wyf.ytl.control");
switch(v.getId()){
case R.id.start:
intent.putExtra("ACTION", 1);
sendBroadcast(intent);
break;
case R.id.stop:
intent.putExtra("ACTION", 2);
sendBroadcast(intent);
break;
}
}
protected void onDestory(){
super.onDestroy();
Intent intent = new Intent(this,MyService.class);
stopService(intent);
}
@Override
public boolean onCreateOptionsMenu (Menu menu){
menu.add(0,Menu.FIRST,0,"退出");
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case Menu.FIRST:
showDialog(1);
break;
}
return false;
}
@Override
public Dialog onCreateDialog(int id){
switch(id){
case 1:
return new AlertDialog.Builder(this).setTitle("您确定退出吗?").setPositiveButton("确定", new android.content.DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
System.exit(0);
}
}).setNegativeButton("取消", null).create();
default:
return null;
}
}
}
然后是ServiceActivity,主要是onCreat和onClick函数,onCreate中是很重要的,其他的主要是写的退出的对话框。onCreate中首先是给按钮添加监听器,然后创建IntentFilter过滤器,添加Action,注册Action,创建Intent,然后启动后台Service