80,350
社区成员
发帖
与我相关
我的任务
分享
package com.min.localservicedemo;
public interface ICountService {
public abstract int getCount();
}
package com.min.localservicedemo;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class CountService extends Service implements ICountService {
private static final String TAG = "CountService";
private boolean threadDisable;
private int count;
private ServiceBinder serviceBinder = new ServiceBinder();
public class ServiceBinder extends Binder implements ICountService {
public int getCount() {
// TODO Auto-generated method stub
return count;
}
}
public int getCount() {
// TODO Auto-generated method stub
return count;
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return serviceBinder;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
while(!threadDisable) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
Log.d(TAG, "count is " + count);
updateCount();
}
}
}
).start();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
threadDisable = true;
Log.d(TAG, "onDestroy");
}
/**
* Update view
*/
public void updateCount() {
LocalServiceDemo.getMyHandler().sendEmptyMessage(1);
}
}
package com.min.localservicedemo;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
public class LocalServiceDemo extends Activity {
private static final String TAG = "LocalServiceDemo";
private static TextView tvCount;
private static ICountService countService;
private static MyHandler myHandler = MyHandler.getInstance();
private ServiceConnection serviceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
countService = (ICountService)service;
Log.d(TAG, "onServiceConnected count is " + countService.getCount());
tvCount.setText("Count:" + countService.getCount());
}
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
countService = null;
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvCount = (TextView)findViewById(R.id.tvCount);
this.bindService(new Intent("com.min.localservicedemo.CountService"),
serviceConnection, BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
this.unbindService(serviceConnection);
Log.d(TAG, "onDestroy unbindService");
}
public static class MyHandler extends Handler {
private static MyHandler myHandler = null;
private MyHandler() {
}
/**
* Single Instance
* @return
*/
public static MyHandler getInstance() {
if (myHandler == null) {
myHandler = new MyHandler();
}
return myHandler;
}
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if(msg.what == 1) {
tvCount.setText("Count:" + countService.getCount());
}
super.handleMessage(msg);
}
}
public static MyHandler getMyHandler() {
return myHandler;
}
}
public static ServiceToken bindToService(Activity context, ServiceConnection callback) {
Activity realActivity = context.getParent();
if (realActivity == null) {
realActivity = context;
}
ContextWrapper cw = new ContextWrapper(realActivity);
cw.startService(new Intent(cw, MediaPlaybackService.class));
ServiceBinder sb = new ServiceBinder(callback);
if (cw.bindService((new Intent()).setClass(cw, MediaPlaybackService.class), sb, 0)) {
sConnectionMap.put(cw, sb);
return new ServiceToken(cw);
}
Log.e("Music", "Failed to bind to service");
return null;
}
tvCount = (TextView)findViewById(R.id.tvCount);
startService(new Intent("com.min.localservicedemo.CountService"));
this.bindService(new Intent("com.min.localservicedemo.CountService"),
serviceConnection, BIND_AUTO_CREATE);