我想在服务中实例化一个线程,即使客户端离开应用程序也能工作。在他返回时,Tread将不再被实例化,只是它的某些参数可能被改变。所以我需要能够访问它。它还涉及屏幕的旋转。
当前代码启动另一个线程,这显然不是一个理想的解决方案。注释掉的东西显示了不同的方法,但它们也没有用。我也必须在main活动中访问threadService对象。
public class MainActivity extends ActionBarActivity {
private static ThreadService threadService;
private ServiceConnection threadConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
ThreadServiceBinder binder = (ThreadServiceBinder) service;
threadService = binder.getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, ThreadService.class);
this.startService(intent);
/* if(threadConnection == null) {
Intent intent = new Intent(this, ThreadService.class);
this.startService(intent);
Toast.makeText(this, "Thread service is null", Toast.LENGTH_SHORT).show();
} else {
if(!threadService.ifThreadIsRunning()) {
Intent intent = new Intent(this, ThreadService.class);
this.startService(intent);
Toast.makeText(this, "Thread service is not null", Toast.LENGTH_SHORT).show();
}
}*/
}
@Override
public void onDestroy() {
this.stopService(new Intent(this, ThreadService.class));
super.onDestroy();
}
/*
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager
.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}*/
}
这是服务
public class ThreadService extends Service {
private final IBinder threadServiceBinder = new ThreadServiceBinder();
public class ThreadServiceBinder extends Binder {
ThreadService getService() {
return ThreadService.this;
}
}
final Handler handler = new Handler();
private BackgroundThread backgroundThread;
public ThreadService() {
/*
* if(backgroundThread == null) backgroundThread =new
* BackgroundThread(handler);
*/
backgroundThread = BackgroundThread.getInstance();
backgroundThread.setHandler(handler);
handler.removeCallbacks(backgroundThread);
handler.postDelayed(backgroundThread, 10000);
}
@Override
public IBinder onBind(Intent intent) {
return threadServiceBinder;
}
public boolean ifThreadIsRunning() {
if (backgroundThread.isAlive()) {
return true;
} else {
return false;
}
}
}
class BackgroundThread extends Thread {
private static BackgroundThread instance;
private Handler mHandler;
private BackgroundThread() {
}
public static synchronized BackgroundThread getInstance() {
if (instance == null) {
instance = new BackgroundThread();
}
return instance;
}
public void setHandler(Handler handler) {
if (mHandler == null) {
mHandler = handler;
}
}
/*
* public BackgroundThread(Handler handler) { super(); mHandler = handler; }
*/
@Override
public void run() {
try {
Log.d("com.example.timer", "We are in Runnable run try section");
mHandler.postDelayed(this, 10000);
} catch (Exception e) {
// TODO: handle exception
}
/*
* finally{ handler.postDelayed(runable, 2000); }
*/
}
}