Skip to content
This repository was archived by the owner on Jun 23, 2022. It is now read-only.

Commit 0b532cd

Browse files
author
Lyla
committed
6.03 Implementing a Sync Adapter
1 parent 886658c commit 0b532cd

File tree

10 files changed

+270
-365
lines changed

10 files changed

+270
-365
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
<!-- This permission is necessary in order for Sunshine to perform network access. -->
66
<uses-permission android:name="android.permission.INTERNET" />
77

8+
<!-- Permissions required by the sync adapter -->
9+
<uses-permission
10+
android:name="android.permission.READ_SYNC_SETTINGS"/>
11+
<uses-permission
12+
android:name="android.permission.WRITE_SYNC_SETTINGS"/>
13+
<uses-permission
14+
android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
15+
816
<application
917
android:allowBackup="true"
1018
android:icon="@mipmap/ic_launcher"
@@ -39,10 +47,32 @@
3947
</activity>
4048
<provider
4149
android:authorities="com.example.android.sunshine.app"
42-
android:name=".data.WeatherProvider" />
50+
android:name=".data.WeatherProvider"
51+
android:exported="false"
52+
android:syncable="true" />
53+
54+
<!-- SyncAdapter's dummy authentication service -->
55+
<service android:name=".sync.SunshineAuthenticatorService">
56+
<intent-filter>
57+
<action android:name="android.accounts.AccountAuthenticator" />
58+
</intent-filter>
59+
<meta-data
60+
android:name="android.accounts.AccountAuthenticator"
61+
android:resource="@xml/authenticator" />
62+
</service>
4363

44-
<service android:name=".service.SunshineService"/>
45-
<receiver android:name=".service.SunshineService$AlarmReceiver" android:enabled="true"/>
64+
<!-- The SyncAdapter service -->
65+
<service
66+
android:name=".sync.SunshineSyncService"
67+
android:exported="true"
68+
>
69+
<intent-filter>
70+
<action android:name="android.content.SyncAdapter" />
71+
</intent-filter>
72+
<meta-data
73+
android:name="android.content.SyncAdapter"
74+
android:resource="@xml/syncadapter" />
75+
</service>
4676
</application>
4777

4878
</manifest>

app/src/main/java/com/example/android/sunshine/app/ForecastFragment.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
*/
1616
package com.example.android.sunshine.app;
1717

18-
import android.app.AlarmManager;
19-
import android.app.PendingIntent;
20-
import android.content.Context;
21-
import android.content.Intent;
2218
import android.database.Cursor;
2319
import android.net.Uri;
2420
import android.os.Bundle;
@@ -36,7 +32,7 @@
3632
import android.widget.ListView;
3733

3834
import com.example.android.sunshine.app.data.WeatherContract;
39-
import com.example.android.sunshine.app.service.SunshineService;
35+
import com.example.android.sunshine.app.sync.SunshineSyncAdapter;
4036

4137
/**
4238
* Encapsulates fetching the forecast and displaying it as a {@link ListView} layout.
@@ -184,16 +180,9 @@ void onLocationChanged( ) {
184180
}
185181

186182
private void updateWeather() {
187-
Intent alarmIntent = new Intent(getActivity(), SunshineService.AlarmReceiver.class);
188-
alarmIntent.putExtra(SunshineService.LOCATION_QUERY_EXTRA, Utility.getPreferredLocation(getActivity()));
189-
190-
//Wrap in a pending intent which only fires once.
191-
PendingIntent pi = PendingIntent.getBroadcast(getActivity(), 0,alarmIntent,PendingIntent.FLAG_ONE_SHOT);//getBroadcast(context, 0, i, 0);
192-
193-
AlarmManager am=(AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
194-
195-
//Set the AlarmManager to wake up the system.
196-
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pi);
183+
//String location = Utility.getPreferredLocation(getActivity());
184+
//new FetchWeatherTask(getActivity()).execute(location);
185+
SunshineSyncAdapter.syncImmediately(getActivity());
197186
}
198187

199188
@Override

0 commit comments

Comments
 (0)