Use SyncAdapter to sync with *local* gmail and facebook app data

百般思念 提交于 2019-11-30 23:59:49

问题


For an app I need to develop, I need to be able to sync with both gmail and facebook data using a SyncAdapter. I'm hoping to sync with the local facebook and gmail apps (if available) instead their remote servers. Syncing with the local apps would mean less battery usage, less network usage, faster syncing, etc. Of course, if needed, I will set the correct permissions.

My questions:

  • If any of the apps (gmail/facebook) are available, is it possible to query them for data (messages, events, etc.)?
  • If not, is it possible to get an authentication token (or any kind of credential) from them that will allow me to query the server without the user needing to put in a username and password of each of them again?
  • If that's possible, is it also possible to automatically sync immediately after either of the facebook or gmail apps sync, so that I get new data as soon as they do?

If none of these are available, I guess I'll need to fetch the data myself, so that's not relevant for this question.


回答1:


I can't speak for Facebook, but the GMail app doesn't allow this. It used to work previously, but they have since secured it so that only Google apps can query the email content provider. The best you can do is get the number of messages with the labels API. You can get an authentication token (ClientLogin or OAuth2) for Google services using the standard Android AccountManager. You will then have to use it to connect to the particular API to get the actual data.




回答2:


Ah, now I've better understood your question. I don't think that there's any way to get the data you're interested in directly from the local copies of the apps. It's an interesting idea, and reasonable for the speed/space/bandwidth reasons you suggest, but would require significant support from the apps that's not currently available.

Trying to avoid the user re-entering their password is also something that would almost certainly still be impossible even if the apps did support sharing local data. After all, how is the GMail app going to verify that your app should have permission to read all of its data? Presumably by prompting for your GMail username and password. Merely being on the same device definitely doesn't imply complete trust.

Using a SyncAdapter and storing the authentication token as demonstrated in the sample project, you should be able to limit the user to entering their credentials once, but that's about the best you're going to be able to do.


Previously:

Have you had a look at the SampleSyncAdapter included with the SDK?

These instructions give a quick overview of how to access the samples.

If you're using Eclipse, it's actually even easier then they suggest there. To get the sample project:

  1. Select File > New > Other...
  2. Choose Android > Android Sample Project
  3. Choose whichever target/API Level is applicable to your project (probably the latest)
  4. Select the sample project you're interested in, SampleSyncAdapter in this case

The sample project is a great example of how to use SyncAdapters and should be readily adaptable to connect with GMail, Facebook, etc.



来源:https://stackoverflow.com/questions/11532257/use-syncadapter-to-sync-with-local-gmail-and-facebook-app-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!