随写...

导航

 

原文地址:android之Service(1)startService作者:sean

运行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 class service extends Service {
 
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
 
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.i("Service","Oncreate");
}
 
@Override
public void onDestroy() {
// TODO Auto-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 void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Log.i("Service","OnStart");
MediaPlayer m=new MediaPlayer();
try{
m.setDataSource("/sdcard/test.mp3");
m.prepare();
m.start();
}
catch(Exception e)
{
Log.i("播放音乐", e.getMessage());
}
}
 
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.i("Service","OnStartCommand");
return super.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
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button start=(Button)findViewById(R.id.button1);
        Button stop=(Button)findViewById(R.id.button2);
        start.setOnClickListener(lis);
        stop.setOnClickListener(lis);
    }
    private Button.OnClickListener lis=new Button.OnClickListener()
    {
 
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=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;
}
}
    
    };
}
 
android之Service(1)startService

android之Service(1)startService
android之Service(1)startService
android之Service(1)startService
android之Service(1)startService

android之Service(1)startService

android之Service(1)startService

posted on 2012-10-24 15:50  mimi51  阅读(272)  评论(0)    收藏  举报