Issue
I am learning how to use services correctly and am running into an issue. I just have a test application that will start a service to increment an integer. I was able to use a messenger to update the UI as the integer increments. Furthermore I was able to set the launch mode of my original activity to singleTask, which allowed me to go back to the instance of the main activity using the pending intent in my service.
The issue I am having is how do I go about reattaching a new activity (really I am focused on getting to the new UI view) to my service after the old one was destroyed (like as part of a screen rotation, exit app, etc.)?
What happens is after the screen rotates or the activity is destroyed the UI when reopened is just the way it was when the application starts, but I can see that my service is still running.
Below is my code for the service:
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;
}
}
This is the code to my main activity:
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();
}
}
Solution
You should setup a shared Handler where the current Activity will register itself as the Callback for the Handler.
Answered By - stuckless
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.