• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Is it possible to call Activity.finish() inside the IntentService?

I am creating a prototype for my initial thesis.

First, my application used Asynctask to do the background to do data retrieval from online and loading the retrieved data to my local DB.
But I decided to change it using IntentService.

Now, my new class derived from IntentService is not a nested class inside my Activity. Thus, I can't Activity.finish() which I need to call if there is something wrong inside the IntentService.

Is it possible to call the finish() of the calling Activity inside the IntentService?

I am thinking of using the argument intent but don't know on how to do it.

Code:
protected void onHandleIntent(Intent intent) {...}

Any guidance on the right direction is appreciated.

Eros
Japan
 
Where does the intent originate? And how is the activity started?



if you pass a reference to the activity in the intent, then you can call finish() on the reference. If you start the activity from the service, you can call the activity by ID I think.

public void finishActivity (int requestCode)

Since: API Level 1

Force finish another activity that you had previously started with startActivityForResult(Intent, int).

Parameters

requestCode The request code of the activity that you had given to startActivityForResult(). If there are multiple activities started with this request code, they will all be finished.


You would need to make the Activity implment parcelable, though

How to send an object from one Android Activity to another using Intents? - Stack Overflow
 
>Where does the intent originate? And how is the activity started?
Code:
public class StartActivity extends Activity {
    @Override
    public void onStart()
    {
        super.onStart();

        Intent intentService = new Intent(this, mobileapp.activity.service.GetDataIntentService.class);
        startService(intentService);

        Intent intent = new Intent(StartActivity.this, StartDialogActivity.class);
        intent.putExtra(ConstData.INTENT_KEY_NAVI_MODE, ConstData.INTENT_FIRST_CALL_START_DIALOG);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivityForResult(intent, 0);
    }
}

public class GetDataIntentService extends IntentService {
    private final String LOGCAT_TAG = "GetDataIntentService";
    
    public GetDataIntentService() {
        super(LOGCAT_TAG);
    }
    
    /* (non-Javadoc)
     * @see android.app.IntentService#onHandleIntent(android.content.Intent)
     */
    @Override
    protected void onHandleIntent(Intent intent) {
        Log.i(LOGCAT_TAG, "GetDataIntentService Start.");
        try {
            ...
        } catch (Exception e) {
            Log.e("..");
            // before it is nested class inside the activity
            // thus, allow to call finish() method.
            //finish();
        }
        Log.i(LOGCAT_TAG, "GetDataIntentService End.");
    }
}
 
Thanks for the link..
But.. wasn't able to find about calling methods of the calling Activity.

Before I used Asynctask as nested class of the calling Activity.
That's why I am free to call Activity.finish() method.

Now, the Asynctask class was replaced by IntentService as separate class (not inside of the calling Activity). Because of that, I can't call the finish() method.

Is it possible to call finish() method inside the IntentService?

OR

Is there a way to end the calling Activity inside the IntentService?
 
The problem is you need a reference (an object) of that activity. The service does not have that by default.

But in the tutorial link above, they show you a way the service can broadcast another intent when it's finished. This other intent can THEN tell your activity to call finnish();

Look at step #5 and #7 :)
 
The problem is you need a reference (an object) of that activity. The service does not have that by default.

But in the tutorial link above, they show you a way the service can broadcast another intent when it's finished. This other intent can THEN tell your activity to call finnish();

Look at step #5 and #7 :)

I see, so there are two ways. Then, using broadcastreceiver is the recommended way. Isn't correct?
 
it seems like the best way from what I can tell.... what I was saying before about passing a reference may not even work.

But the broadcast receiver way the tutorial uses seems correct (and flexible)
 
it seems like the best way from what I can tell.... what I was saying before about passing a reference may not even work.

But the broadcast receiver way the tutorial uses seems correct (and flexible)

Yeah, thanks. I read the documentation about using the broadcast receiver, it seems a callback then pass the result via intent.

1) However my result are complex object, is there a way to pass it via intent?
2) How about the performance of sending a broadcast repeatedly. About 500ms of interval?
 
Back
Top Bottom