Created
September 7, 2014 13:22
-
-
Save nein37/cefc4ac3f2574b55a855 to your computer and use it in GitHub Desktop.
SyncAdapterの実装メモ ref: http://qiita.com/nein37/items/cf664d0078a6276cf3d6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.example.nein37.syncadaptersample" > | |
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> | |
<uses-permission android:name="android.permission.USE_CREDENTIALS" /> | |
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" /> | |
<application | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name"> | |
<activity | |
android:name=".MyActivity" | |
android:label="@string/app_name" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
<provider | |
android:exported="false" | |
android:syncable="true" | |
android:authorities="com.example.nein37.syncadaptersample" | |
android:name=".MyContentProvider" /> | |
<service | |
android:name=".MySyncService" | |
android:exported="true" | |
android:process=":sync"> | |
<intent-filter> | |
<action android:name="android.content.SyncAdapter" /> | |
</intent-filter> | |
<meta-data | |
android:name="android.content.SyncAdapter" | |
android:resource="@xml/syncadapter" /> | |
</service> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void onClick(View v) { | |
Account account = AccountManager.get(this).getAccountsByType("com.google")[0]; | |
ContentResolver.requestSync(account,SampleColumn.AUTHORITY,new Bundle()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_my); | |
// 今回は最初に登録されたGoogleアカウントが対象 | |
Account account = AccountManager.get(this).getAccountsByType("com.google")[0]; | |
// ネットワークメッセージタイミングで同期 | |
ContentResolver.setSyncAutomatically(account, AUTHORITY, true); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MySyncAdapter extends AbstractThreadedSyncAdapter { | |
ContentResolver mContentResolver; | |
public MySyncAdapter(Context context, boolean autoInitialize) { | |
super(context, autoInitialize); | |
mContentResolver = context.getContentResolver(); | |
} | |
@Override | |
public void onPerformSync(Account account, Bundle extras, String authority, | |
ContentProviderClient provider, SyncResult syncResult) { | |
try { | |
AccountManager manager =AccountManager.get(getContext()); | |
// トークンの取得 | |
String token = manager.blockingGetAuthToken(account, "cl", true); | |
// TODO ここで通信処理を行う | |
// TODO DBへの反映処理を行う | |
ContentValues values = new ContentValues(); | |
values.put(COLUMN, VALUE); | |
getContext().getContentResolver().insert(CONTENT_URI, values); | |
} catch (OperationCanceledException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (AuthenticatorException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MySyncService extends Service { | |
private MySyncAdapter mSyncAdapter; | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
mSyncAdapter =new MySyncAdapter(this,true); | |
} | |
@Override | |
public IBinder onBind(Intent intent) { | |
return mSyncAdapter.getSyncAdapterBinder(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android" | |
android:accountType="com.google" | |
android:allowParallelSyncs="false" | |
android:contentAuthority="com.example.nein37.syncadaptersample" | |
android:isAlwaysSyncable="true" | |
android:supportsUploading="false" | |
android:userVisible="true" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment