0

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. I.e how to get notified once the activity is closed in Syncadapter.

Mike Precup
  • 4,148
  • 21
  • 41
Naruto
  • 9,476
  • 37
  • 118
  • 201

3 Answers3

1

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!

  • HI, thanks lot. But i want once the pending intent authorization is completed a callback. i.e i will get authorization screen, i will authorize the service and once that is closed i need to resume from the point i started the sync activity – Naruto Jun 26 '14 at 17:32
  • but, when a exception occours, the syncAdapter continues to run, so SyncAdapter thread will finish. You can try the following: when a sync method generate a exception, you get a notification and you start another Sync activity of in the next method with a Bundle. ContentResolver.requestSync(account, CONTENT_AUTHORITY, bundle); Did you understand me? If you want, I will post another answer with code. I hope that works. – Ighor Augusto Jun 26 '14 at 18:10
  • Hi, i didnt get u. i just want to know how to restart the sync once the notification activity is completed. can you please post a piece of code. Please thanks lot for supporting – Naruto Jun 26 '14 at 18:13
  • Look my another answer! And say me if it helps you! – Ighor Augusto Jun 26 '14 at 18:26
  • Hi Thank u, acutally authorization part is inbuilt one i.e http://javadoc.google-api-java-client.googlecode.com/hg/1.12.0-beta/com/google/api/client/googleapis/extensions/android/gms/auth/UserRecoverableAuthIOException.html. Here there is a inbuilt intent gets called once i get exception. with help of this directly i'm launching a pending intent. i dont have authorization intent in my control :( – Naruto Jun 26 '14 at 18:32
  • when exception occours you call activity: myActivity.startActivityForResult(userRecoverableException.getIntent(), MyActivity.REQUEST_AUTHORIZATION); so, in this activity you call ContentResolver.requestSync(account, CONTENT_AUTHORITY, b) like I show you in my other answer. – Ighor Augusto Jun 26 '14 at 18:39
  • But from the thread syncadapter thread i cant typecast activity, it will throw error :( – Naruto Jun 26 '14 at 18:41
  • how so? for which you want typecast activity? – Ighor Augusto Jun 26 '14 at 18:55
  • when a exception occours you can send a broadcast with: context.sendBroadcast(i), to comunicate with Activity or you can send a notification with: mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()) to notify the crash to user! – Ighor Augusto Jun 26 '14 at 19:03
  • if you want, send me a piece of your code, and I will can help you better! my e-mail: ighorabcandido@hotmail.com – Ighor Augusto Jun 26 '14 at 19:06
  • Hi, I will post you code, however i will clear one more time the scenario. 1) From your phone accounts and sync i will go to a google account, there the sync content provider will be present. 2) There i will press on Sync, since for the first time the sync provider do not have access to my Google drive, tasks, calender etc.. accounts it raises ioexception as stated above. 3) With help of Notification provider i will raise a notification in my catch block. 4) once user click on notification it will open a authentication window. 5) Now how to re initiate the sync once user completes the auth par – Naruto Jun 27 '14 at 09:22
  • i have also created a new question here, http://stackoverflow.com/questions/24448584/re-initate-sync-adapter-in-android. However i will create a sample app, and send u code later. Please help thanks – Naruto Jun 27 '14 at 09:38
  • hey man. I read your new question. Before I post my answer, let me see if I understand: You started the sync service, and an exception has occourred, you push a notification for user, and this notification opens a Login Dialog for user, after user enter his credentials, you want to restart or resume sync service, right? The push notification can you already do? – Ighor Augusto Jun 27 '14 at 12:33
  • Yes, you are right. I'm very very thankful for your help. Mean time i'm also doing some R&D, i will let you know if i get any hint. Thanks lot dear, Thank you very much – Naruto Jun 27 '14 at 14:58
  • Hey i found the solution, here is the link http://stackoverflow.com/questions/14828998/how-to-show-sync-failed-message. We just need to increment the exception count and we can ask system to retry sync after specific time in delayUntil param. – Naruto Jun 27 '14 at 15:37
  • I'm very very thankful for your help. i will be giving 2 +1 for your 2 answer. Though it will help in another way. Thanks lot – Naruto Jun 27 '14 at 15:38
  • You welcome, I'm very happy you found the answer. Thank you for give me 2 +1. Greetings – Ighor Augusto Jun 27 '14 at 17:57
1

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!!!

0

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

Community
  • 1
  • 1
Naruto
  • 9,476
  • 37
  • 118
  • 201