我正在学习如何正确使用服务并且遇到了问题。我只有一个测试应用程序,它将启动一个服务来增加一个整数。随着整数的增加,我能够使用信使来更新 UI。此外,我能够将原始活动的启动模式设置为 singleTask,这允许我使用服务中的待处理意图返回到主要活动的实例。
我遇到的问题是如何在旧活动被销毁后(例如作为屏幕旋转、退出应用程序等的一部分)将新活动(实际上我专注于进入新的 UI 视图)重新附加到我的服务.)?
屏幕旋转或活动被破坏后会发生什么,重新打开时的 UI 与应用程序启动时一样,但我可以看到我的服务仍在运行。
以下是我的服务代码:
public class BackgroundService extends Service {
private static final String TAG = "BackgroundService";
private NotificationManager notificationMgr;
int counter;
// use regular thread
private ThreadGroup myThreads = new ThreadGroup("ServiceWorker");
public void onCreate() {
super.onCreate();
notificationMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
displayNotificationMessage("Background Service is running");
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
int counter = intent.getExtras().getInt("counter");
Messenger msg = (Messenger) intent.getExtras().get("msg_JIB");
new Thread(myThreads, new ServiceWorker(counter, msg), "BackgroundService")
.start();
return START_NOT_STICKY;
}
class ServiceWorker implements Runnable
{
int counter = -1;
private Messenger msg1 = null;
public ServiceWorker(int counter, Messenger msg) {
this.counter = counter;
this.msg1 = msg;
}
public void run() {
final String TAG2 = "ServiceWorker:" + Thread.currentThread().getId();
// do background processing here...
try {
while (counter<100){
Message message = Message.obtain();
message.arg1=counter;
msg1.send(message);
counter = counter +1;
Thread.sleep(5000);
}
} catch (Throwable e) {
Log.v(TAG2, "... sleep interrupted");
}
}
}
private void displayNotificationMessage(String message) {
Notification notification = new Notification(R.drawable.emo_im_winking,
message, System.currentTimeMillis());
Intent i = new Intent(this, ProDroid_Android_CH11_Local_ServiceActivity.class);
//i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
i, 0);
notification.setLatestEventInfo(this, TAG, message, contentIntent);
notification.flags |= Notification.FLAG_NO_CLEAR;
notificationMgr.notify(0, notification);
}
@Override
public void onDestroy()
{
Log.v(TAG, "in onDestroy(). Interrupting threads and cancelling notifications");
myThreads.interrupt();
notificationMgr.cancelAll();
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
这是我的主要活动的代码:
public class ProDroid_Android_CH11_Local_ServiceActivity extends Activity {
private static final String TAG = "MainActivity";
private int counter = 1;
Handler handler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("Count: " + msg.arg1);
};
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
public void doClick(View view) {
switch (view.getId()) {
case R.id.startBtn:
Log.v(TAG, "Starting service... counter = " + counter);
Intent intent = new Intent(
ProDroid_Android_CH11_Local_ServiceActivity.this,
BackgroundService.class);
intent.putExtra("counter", counter);
intent.putExtra("msg_JIB", new Messenger(handler));
startService(intent);
break;
case R.id.stopBtn:
stopService();
}
}
private void stopService() {
Log.v(TAG, "Stopping service...");
if (stopService(new Intent(
ProDroid_Android_CH11_Local_ServiceActivity.this,
BackgroundService.class)))
Log.v(TAG, "stopService was successful");
else
Log.v(TAG, "stopService was unsuccessful");
}
@Override
public void onDestroy() {
//stopService();
super.onDestroy();
}
}
任何帮助,将不胜感激。
谢谢