startService与bindService


转自:http://blog.sina.com.cn/s/blog_695bab300100ulr3.html

运行service的方法有两种startService和bindService,区别startService的调用者和serice没有联系,及调用者结束后,service依然继续运行,而bindService组件结束后,service也结束。

该文主要讲的是使用startService启动服务。
在startService调用方法中,service的生命周期:
onCreate()->onStratCommand()->onStart()->服务->onDestroy()
说明:系统中不存在服务时,才执行oncreate方法,否则直接执行onStartCommand

首先创建服务程序,继承service基类
//文件名:servicejava
package jun.xiao;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.VideoView;

public classservice extends Service {

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public voidonCreate() {
// TODOAuto-generated method stub
super.onCreate();
Log.i("Service","Oncreate");
}

@Override
public voidonDestroy() {
// TODOAuto-generated method stub
super.onDestroy();
Log.i("Service","OnDestroy");
}

@Override
public void onRebind(Intent intent) {
// TODO Auto-generated method stub
super.onRebind(intent);
}

@Override
public voidonStart(Intent intent, int startId) {
// TODOAuto-generated method stub
super.onStart(intent, startId);
Log.i("Service","OnStart");
MediaPlayerm=new MediaPlayer();
try{
m.setDataSource("/sdcard/test.mp3");
m.prepare();
m.start();
}
catch(Exceptione)
{
Log.i("播放音乐",e.getMessage());
}
}

@Override
public intonStartCommand(Intent intent, int flags, int startId){
// TODOAuto-generated method stub
Log.i("Service","OnStartCommand");
returnsuper.onStartCommand(intent, flags, startId);
}

@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
return super.onUnbind(intent);
}

}

创建activity,调用startService方法
//文件名:xiaotech.java
package jun.xiao;

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

public class xiaotech extends Activity {
     
      @Override
      publicvoid onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          Buttonstart=(Button)findViewById(R.id.button1);
          Buttonstop=(Button)findViewById(R.id.button2);
          start.setOnClickListener(lis);
          stop.setOnClickListener(lis);
      }
      privateButton.OnClickListener lis=new Button.OnClickListener()
      {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intentintent=new Intent(xiaotech.this,service.class);
switch(v.getId())
{
case R.id.button1:
startService(intent);
Toast.makeText(xiaotech.this, "开启服务",Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
stopService(intent);
Toast.makeText(xiaotech.this, "停止服务",Toast.LENGTH_SHORT).show();
break;
}
}
     
      };
}

startService与bindService_第1张图片

startService与bindService_第2张图片
[转载]android之Service(1)startService
[转载]android之Service(1)startService
startService与bindService_第3张图片

startService与bindService_第4张图片

[转载]android之Service(1)startService

你可能感兴趣的:(service,null,Class,音乐,button)