简体   繁体   中英

How to pause and resume thread in android

When I get UserRecoverableAuthIOException in AbstractThreadedSyncAdapter , I'm creating a notification as below.

Here's how I'm creating the service:

@Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub Log.i("Service", "Thread started"); return this.getSyncAdapter().getSyncAdapterBinder(); }

private GoogleTasksSyncAdapter getSyncAdapter() {
    if (syncAdapter == null)
    {
        Log.i("Service", "syncAdapter started");
        syncAdapter = new MySyncAdapter(this);

    }
    return syncAdapter;

}

Once the thread is started, I'm raising a notification. But once user clicks on the notification, they can see the authorization activity. After authorising how to resume from the last point. Ie how to get notified once the activity is closed in Syncadapter.

The SyncAdapter thread are running, and you want to get notification when SyncAdapter ends, right?

So, you can comunicate the SyncAdapter thread with BroadCast.

In your SyncAdapter class:

Intent i = new Intent(SYNC_FINISHED);
    context.sendBroadcast(i);
    Log.i(TAG, "Network synchronization complete");

In a activity or a fragment:

private BroadcastReceiver syncFinishedReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "Sync finished!!");

        // Here you can send your notification or another thing that you want
    }
};

@Override
public void onStart() {
    super.onStart();
    getActivity().registerReceiver(syncFinishedReceiver, new IntentFilter(SyncAdapter.SYNC_TASK_FINISHED));
}

@Override
public void onStop() {
    super.onStop();
    getActivity().unregisterReceiver(syncFinishedReceiver);
}

NOTE: The SYNC_FINISHED constant, you can define previously in your SyncAdapter

I hope I've helped you.

Greetings!

In your SyncAdapter you do something like:

    @Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {

    Log.i(TAG, "Beginning network synchronization");

    if(extras.getBoolean(RUN_METHOD_1) || extras.getBoolean(RUN_ALL)) {
        method1();
    }
    if(extras.getBoolean(RUN_METHOD_2) || extras.getBoolean(RUN_ALL)) {
        method2();
    }
}

public void method1(){
  try{
        // do something

     } catch (Exception e) {
       e.printStackTrace();

       // here you can send your notification when exception occours.
     }
}

public void method2(){
  try{
        // do something

     } catch (Exception e) {
       e.printStackTrace();

       // here you can send your notification when exception occours.
     }
}

in your "authorization" code you do something like:

        Bundle b = new Bundle();
        b.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
        b.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
        b.putBoolean(SyncAdapter.RUN_METHOD_1, true);

        ContentResolver.requestSync(account, CONTENT_AUTHORITY, b); 

so you can run where the sync stopped.

Greetings!!!

Here is the solution,

we need to use the syncResult.stats.numAuthExceptions to tell about exception, it throws message automatically. syncResult.delayUntil will wait and restart sync after elapsing time

How to show sync failed message

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM