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

Commit 886658c

Browse files
author
Lyla
committed
6.02 Using Alarms
1 parent dd2aee0 commit 886658c

3 files changed

Lines changed: 27 additions & 4 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
android:name=".data.WeatherProvider" />
4343

4444
<service android:name=".service.SunshineService"/>
45+
<receiver android:name=".service.SunshineService$AlarmReceiver" android:enabled="true"/>
4546
</application>
4647

4748
</manifest>

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

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

18+
import android.app.AlarmManager;
19+
import android.app.PendingIntent;
20+
import android.content.Context;
1821
import android.content.Intent;
1922
import android.database.Cursor;
2023
import android.net.Uri;
@@ -181,10 +184,16 @@ void onLocationChanged( ) {
181184
}
182185

183186
private void updateWeather() {
184-
Intent intent = new Intent(getActivity(), SunshineService.class);
185-
intent.putExtra(SunshineService.LOCATION_QUERY_EXTRA,
186-
Utility.getPreferredLocation(getActivity()));
187-
getActivity().startService(intent);
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);
188197
}
189198

190199
@Override

app/src/main/java/com/example/android/sunshine/app/service/SunshineService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package com.example.android.sunshine.app.service;
1818

1919
import android.app.IntentService;
20+
import android.content.BroadcastReceiver;
2021
import android.content.ContentUris;
2122
import android.content.ContentValues;
23+
import android.content.Context;
2224
import android.content.Intent;
2325
import android.database.Cursor;
2426
import android.net.Uri;
@@ -331,4 +333,15 @@ long addLocation(String locationSetting, String cityName, double lat, double lon
331333
return locationId;
332334
}
333335

336+
public static class AlarmReceiver extends BroadcastReceiver {
337+
338+
@Override
339+
public void onReceive(Context context, Intent intent) {
340+
Intent sendIntent = new Intent(context, SunshineService.class);
341+
sendIntent.putExtra(SunshineService.LOCATION_QUERY_EXTRA, intent.getStringExtra(SunshineService.LOCATION_QUERY_EXTRA));
342+
context.startService(sendIntent);
343+
344+
}
345+
}
346+
334347
}

0 commit comments

Comments
 (0)