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

Commit

Permalink
6.03 Implementing a Sync Adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyla committed Mar 4, 2015
1 parent 886658c commit 0b532cd
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 365 deletions.
36 changes: 33 additions & 3 deletions app/src/main/AndroidManifest.xml
Expand Up @@ -5,6 +5,14 @@
<!-- This permission is necessary in order for Sunshine to perform network access. -->
<uses-permission android:name="android.permission.INTERNET" />

<!-- Permissions required by the sync adapter -->
<uses-permission
android:name="android.permission.READ_SYNC_SETTINGS"/>
<uses-permission
android:name="android.permission.WRITE_SYNC_SETTINGS"/>
<uses-permission
android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down Expand Up @@ -39,10 +47,32 @@
</activity>
<provider
android:authorities="com.example.android.sunshine.app"
android:name=".data.WeatherProvider" />
android:name=".data.WeatherProvider"
android:exported="false"
android:syncable="true" />

<!-- SyncAdapter's dummy authentication service -->
<service android:name=".sync.SunshineAuthenticatorService">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator" />
</intent-filter>
<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator" />
</service>

<service android:name=".service.SunshineService"/>
<receiver android:name=".service.SunshineService$AlarmReceiver" android:enabled="true"/>
<!-- The SyncAdapter service -->
<service
android:name=".sync.SunshineSyncService"
android:exported="true"
>
<intent-filter>
<action android:name="android.content.SyncAdapter" />
</intent-filter>
<meta-data
android:name="android.content.SyncAdapter"
android:resource="@xml/syncadapter" />
</service>
</application>

</manifest>
Expand Up @@ -15,10 +15,6 @@
*/
package com.example.android.sunshine.app;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
Expand All @@ -36,7 +32,7 @@
import android.widget.ListView;

import com.example.android.sunshine.app.data.WeatherContract;
import com.example.android.sunshine.app.service.SunshineService;
import com.example.android.sunshine.app.sync.SunshineSyncAdapter;

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

private void updateWeather() {
Intent alarmIntent = new Intent(getActivity(), SunshineService.AlarmReceiver.class);
alarmIntent.putExtra(SunshineService.LOCATION_QUERY_EXTRA, Utility.getPreferredLocation(getActivity()));

//Wrap in a pending intent which only fires once.
PendingIntent pi = PendingIntent.getBroadcast(getActivity(), 0,alarmIntent,PendingIntent.FLAG_ONE_SHOT);//getBroadcast(context, 0, i, 0);

AlarmManager am=(AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);

//Set the AlarmManager to wake up the system.
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pi);
//String location = Utility.getPreferredLocation(getActivity());
//new FetchWeatherTask(getActivity()).execute(location);
SunshineSyncAdapter.syncImmediately(getActivity());
}

@Override
Expand Down

1 comment on commit 0b532cd

@shinelikeamillion
Copy link

@shinelikeamillion shinelikeamillion commented on 0b532cd Nov 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i got an error here: it print like this: E/SyncManager: Bind attempt failed - target. it happened on Android 6.0, i have no idea to deal with it, give me some help, pls. (my english is bad, sorry about that..)


Please sign in to comment.