这个也是很简单,没什么好说的,
不写AIDL那个接口的Service的另一种用法,在后台控制一个进度条,本想把以前学习时弄的一个播放器的例子弄上来,发现还需要整理一下,另一个网上大都是播放器的例子,我那天整理再考虑把我那个播放器的例子弄上来也行。先把以前的笔记弄上来先
下面这是那个后台继承自Service的类,包和类不导了
public class ProgressService extends Service {
Binder binder = new MyBinder();
Handler h,subh;
List<Thread> theTList = new ArrayList<Thread>();
class MyBinder extends Binder //这是另一种方法返回绑定的的Service,这种主要是用在没有写AIDL文件的时候用
public ProgressService getService()
{
return ProgressService.this;
}
}
public Handler getThisHandler()
{
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ((MyThread)theTList.get(theTList.size()-1)).getHandler();
}
public void setMainHandler(Handler h)
{
this.h = h;
((MyThread)theTList.get(theTList.size()-1)).setOutGoingHandler(h);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
theTList.add(new MyThread(h));
Log.i("lan","onBind");
theTList.get(theTList.size()-1).start();
return binder;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
for (Thread t : theTList) {
((MyThread)t).stopThis();
}
super.onDestroy();
}
}
class MyThread extends Thread
{
Handler outGoingHandler,thisHandler;
int progress = 0;
boolean isPause = false;
public MyThread(Handler out) {
// TODO Auto-generated constructor stub
outGoingHandler = out;
}
public void setOutGoingHandler(Handler out)
{
outGoingHandler = out;
}
public Handler getHandler()
{
return thisHandler;
}
@Override
public void run() {
// TODO Auto-generated method stub
Looper.prepare();
thisHandler = new Handler(Looper.myLooper()){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if(msg.what==0)
{
if(isPause)
{
return;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Message.obtain(outGoingHandler, 1, progress, 0).sendToTarget();
Message.obtain(thisHandler, 0).sendToTarget();
progress++;
}
else if(msg.what == 2)
{
this.getLooper().quit();
}
else if(msg.what==4)
{
isPause = true;
}
else
{
}
super.handleMessage(msg);
}
};
Message.obtain(thisHandler, 0).sendToTarget();
Looper.loop();
}
public void stopThis()
{
Message.obtain(thisHandler, 2).sendToTarget();
}
}
下面这个是主界面Activity,在这个类里绑定调用上面那个类
public class Service3Activity extends Activity {
/** Called when the activity is first created. */
Button btStart,btPause,btStop;
ProgressBar pb;
ServiceConnection conn;
ProgressService MyService;
Handler mainHandler,subHandler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pb = (ProgressBar) findViewById(R.id.progressBar1);
btStart = (Button) findViewById(R.id.button1);
btPause = (Button) findViewById(R.id.button2);
btStop = (Button) findViewById(R.id.button3);
pb.setMax(100);
btStart.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
conn = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
MyService = ((ProgressService.MyBinder)service).getService();
MyService.setMainHandler(mainHandler);
subHandler = MyService.getThisHandler();
}
};
bindService(new Intent("com.gaoyunfeng.www.cctv1"), conn, Service.BIND_AUTO_CREATE);
}
});
btStop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
unbindService(conn);
}
});
btPause.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
subHandler.obtainMessage(4).sendToTarget();
}
});
mainHandler = new Handler(getMainLooper()){
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if(msg.what==1)
{
if(msg.arg1>=pb.getMax())
{
Message.obtain(subHandler, 2);
}
pb.setProgress(msg.arg1);
}
super.handleMessage(msg);
}
};
}
}
很简单但还是担心吊胆放上来了,希望大家别扔砖过来,我是菜鸟