Add initial code to show alerts

This commit is contained in:
bspeice 2013-10-09 20:20:04 -04:00
parent 9fc974402b
commit 0fb498a306
6 changed files with 64 additions and 2 deletions

View File

@ -5,7 +5,7 @@
android:versionName="1.0" > android:versionName="1.0" >
<uses-sdk <uses-sdk
android:minSdkVersion="8" android:minSdkVersion="11"
android:targetSdkVersion="17" /> android:targetSdkVersion="17" />
<application <application

View File

@ -5,7 +5,7 @@
android:versionName="1.0" > android:versionName="1.0" >
<uses-sdk <uses-sdk
android:minSdkVersion="8" android:minSdkVersion="11"
android:targetSdkVersion="17" /> android:targetSdkVersion="17" />
<application <application

View File

@ -1,20 +1,37 @@
package com.uncc.gameday.activities; package com.uncc.gameday.activities;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.Editor;
import android.os.Bundle; import android.os.Bundle;
import com.uncc.gameday.R; import com.uncc.gameday.R;
import com.uncc.gameday.alerts.AlertFetcher; import com.uncc.gameday.alerts.AlertFetcher;
import com.uncc.gameday.alerts.AlertService;
public class Home extends MenuActivity { public class Home extends MenuActivity {
private final long alarmRate = 300000; // 5 Minutes
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home); setContentView(R.layout.activity_home);
Context ctx = getApplicationContext(); Context ctx = getApplicationContext();
// Start up the AlarmManager to fetch alerts in the background
AlarmManager am = (AlarmManager) this.getSystemService(ALARM_SERVICE);
Intent alertFetcher = new Intent(this, AlertService.class);
PendingIntent pendingAlertFetcher = PendingIntent.getService(ctx, 0, alertFetcher, 0);
// Cancel any previous alarm managers, and start the new one
am.cancel(pendingAlertFetcher);
am.setRepeating(0, this.alarmRate, this.alarmRate, pendingAlertFetcher);
// Double check if we need to do any first-run code
this.onFirstRun(ctx); this.onFirstRun(ctx);
} }

View File

@ -2,6 +2,16 @@ package com.uncc.gameday.alerts;
import java.util.Date; import java.util.Date;
import com.uncc.gameday.R;
import com.uncc.gameday.activities.Home;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
public class Alert { public class Alert {
private Date alarmDate; private Date alarmDate;
@ -28,4 +38,21 @@ public class Alert {
this.shown = shown; this.shown = shown;
} }
public void displayNotification(Context ctx) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("GameDay Alert")
.setContentText(this.getMessage());
Intent resultIntent = new Intent(ctx, Home.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(ctx);
stackBuilder.addParentStack(Home.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
} }

View File

@ -31,4 +31,9 @@ public class AlertDB {
public void deleteAlerts(List<Alert> alerts) { public void deleteAlerts(List<Alert> alerts) {
// Remove multiple dates from the database // Remove multiple dates from the database
} }
public List<Alert> fetchUnread() {
// Fetch all unread alerts
return null;
}
} }

View File

@ -1,6 +1,11 @@
package com.uncc.gameday.alerts; package com.uncc.gameday.alerts;
import java.util.List;
import com.uncc.gameday.GameDay;
import android.app.IntentService; import android.app.IntentService;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
public class AlertService extends IntentService { public class AlertService extends IntentService {
@ -13,6 +18,14 @@ public class AlertService extends IntentService {
@Override @Override
protected void onHandleIntent(Intent intent) { protected void onHandleIntent(Intent intent) {
// Go fetch all the alerts! // Go fetch all the alerts!
Context appContext = GameDay.getAppContext();
AlertDB dbHandle = new AlertDB();
List<Alert> alerts = dbHandle.fetchUnread();
// And then display all of them!
for (Alert a: alerts) {
a.displayNotification(appContext);
}
} }
} }