offlinefähige apps für android entwickeln (karlsruher entwicklertag 2014)

38
Überleben im Funkloch Offlinefähige Apps für Android entwickeln Christian Janz ( ) @c_janz [email protected]

Upload: christian-janz

Post on 13-Jul-2015

189 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

Überleben imFunkloch

Offlinefähige Apps für Android entwickelnChristian Janz ( )@c_janz

[email protected]

Page 2: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

Twitter: | E-Mail: Slides:

Christian JanzConsultant im Bereich Softwareentwicklung Java/JEE bei

in MannheimBridging IT

Interesse: Architektur und Entwicklung vonGeschäftsanwendungen mit Hilfe moderner Java Frameworks

@c_janz [email protected]://de.slideshare.net/cjanz

Page 3: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

AgendaMotivationLösungsideeArchitekturansatzAPIs im Einsatz: Sync SampleFazit

Page 4: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

Motivation

Page 5: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)
Page 6: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)
Page 7: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)
Page 8: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

1. Juli 2013

Page 9: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

Lösungsidee

Page 10: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

Synchronisiere Daten undspeichere sie lokal auf dem

Gerät

Page 11: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

VorteileApp kann auch ohne aktive Datenverbindung genutzt werdenDatenvolumen wird reduziertAkkulaufzeit wird erhöhtDaten werden automatisch aktualisiert, wenn eineDatenverbindung bestehtRetry bei abgebrochener Verbindung"Nebenbei": Verbesserte Architektur

Page 12: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

Architektur

Page 13: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

"Developing Android REST Client Applications"

Architekturansatz für AndroidVortrag von Virgil Dobjanschi auf der Google IO 2010

Page 14: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

Quelle: Developing Android REST Client Applications

Page 15: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

SyncAdapter: FeaturesBeachten von NetzwerkverfügbarkeitSynchronisation auch wenn App nicht läuftPeriodische SynchronisationWarteschlageBündeln von Syncs: Gut für AkkulaufzeitIntegration in globale Sync-EinstellungenIntegration in Account-Verwaltung

Page 16: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

APIs im Einsatz:Sync Sample

Page 17: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

VorgehenAuthenticator & AccountContentProviderREST clientSyncAdapterUI

Page 19: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

Authenticator & AccountAuthenticatorServiceAuthenticatorLoginActivityauthenticator.xmlAndroidManifest.xml

Page 20: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

authenticator.xml<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android" android:accountType="de.bit.android.sample.account" android:icon="@drawable/ic_launcher" android:smallIcon="@drawable/ic_launcher" android:label="@string/app_name"/>

Page 21: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

AndroidManifest.xml<service android:name="de.bit.android.syncsample.authenticator.AuthenticatorService" > <intent-filter> <action android:name="android.accounts.AccountAuthenticator" /> </intent-filter>

<meta-data android:name="android.accounts.AccountAuthenticator" android:resource="@xml/authenticator" /></service>

<activity android:name="de.bit.android.syncsample.authenticator.LoginActivity" android:excludeFromRecents="true" android:exported="true" android:theme="@android:style/Theme.Holo" ></activity>

Page 22: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

Demo: Authenticator &Account

Page 23: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

ContentProviderTodoContentProviderDatabaseHelperTodoEntityAndroidManifest.xml

Page 24: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

TodoEntity

public class TodoEntity { private Long id; private Long serverId; private Long serverVersion; private Long conflictedServerVersion; private SyncState syncState = SyncState.NOOP;

private String title; private String text;

... (getters and setters) }

Page 25: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

REST clientpublic class TodoRestClient {

public static List<TodoEntity> loadAllTodos() throws IOException, JSONException;

public static TodoEntity saveTodo(TodoEntity todoEntity) throws IOException, JSONException;

public static void deleteTodo(TodoEntity todoEntity) throws IOException;}

Page 26: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

SyncAdapterSyncServiceSyncAdaptersyncadapter.xmlAndroidManifest.xml

Page 27: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

syncadapter.xml<?xml version="1.0" encoding="utf-8"?><sync-adapter xmlns:android="http://schemas.android.com/apk/res/android" android:accountType="de.bit.android.sample.account" android:contentAuthority="de.bit.android.syncsample.content" android:supportsUploading="true" />

Page 28: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

AndroidManifest.xml<service android:name="de.bit.android.syncsample.sync.SyncService" > <intent-filter> <action android:name="android.content.SyncAdapter" /> </intent-filter>

<meta-data android:name="android.content.SyncAdapter" android:resource="@xml/syncadapter" /></service>

Page 29: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

Demo: Speichern der Datenvom Backend

@Overridepublic void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) { ...}

Page 30: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

Periodische Synchronisationprivate void configureSync(Account account) { ContentResolver.setIsSyncable(account, CONTENT_AUTHORITY, 1); ContentResolver.setSyncAutomatically(account, CONTENT_AUTHORITY, true);

Bundle params = new Bundle(); params.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false); params.putBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false); params.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false); ContentResolver.addPeriodicSync(account, CONTENT_AUTHORITY, params, 60);

ContentResolver.requestSync(account, CONTENT_AUTHORITY, params);}

Page 31: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

UIMainActivityactivity_main.xmltodo_row.xmlEditTodoActivityactivity_edit_todo.xmlAndroidManifest.xml

Page 32: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

Demo: Sync-Status anzeigen@Overrideprotected void onResume() { super.onResume();

syncObserverHandle = ContentResolver.addStatusChangeListener( SYNC_OBSERVER_TYPE_ACTIVE | SYNC_OBSERVER_TYPE_PENDING, this);}

@Overridepublic void onStatusChanged(int which) { runOnUiThread(new Runnable() {

@Override public void run() { boolean isSyncActive = ContentResolver.isSyncActive(account, TodoContentProvider.AUTHORITY); setProgressBarIndeterminateVisibility(isSyncActive); } });}

Page 33: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

Demo: Synchronisationanstoßen

Bundle params = new Bundle();params.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);params.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);

ContentResolver.requestSync(account, TodoContentProvider.AUTHORITY,params);

Page 34: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

Demo: Automatisches Updateder UI

CursorLoader und ContentResolver.notifyChange(...)

Page 35: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

Demo: Create, Update, Delete

Page 36: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

Fazit

Page 37: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

Offlinefähige Apps haben VorteileOfflinefähigkeit muss nicht aufwändig seinAndroid bietet gute Unterstützung dafürDokumentation nicht optimalFehlerhandling muss beachtet werden

Page 38: Offlinefähige Apps für Android entwickeln (Karlsruher Entwicklertag 2014)

LinksTransferring Data Using Sync Adapters | Android DevelopersSlides: Android SyncAdapter | Alex TumanoffTutorial: Write your own Android SyncAdapter | UdinicSyncAdapter Sample App | Christian Janz