android-ibc-forum

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 584eb15ec17b4dde5766d616021d4985751e5376
parent bd7be3b732a27e249936cea96e75adec392266e7
Author: Jan Dankert <devnull@localhost>
Date:   Sun, 12 Feb 2012 20:13:33 +0100

Abo-Service nicht starten, wenn Benutzername nicht existiert oder der Service nicht laufen soll. Außerdem nie Fehlermeldungen anzeigen, da in Situationen mit schlechtem Empfang der Benutzer nicht mit Fehlermeldungen genervt werden soll.

Diffstat:
src/de/mtbnews/android/receiver/NetworkStateReceiver.java | 2+-
src/de/mtbnews/android/service/SubscriptionService.java | 45++++++++++++++++++++++++++-------------------
2 files changed, 27 insertions(+), 20 deletions(-)

diff --git a/src/de/mtbnews/android/receiver/NetworkStateReceiver.java b/src/de/mtbnews/android/receiver/NetworkStateReceiver.java @@ -40,7 +40,7 @@ public class NetworkStateReceiver extends BroadcastReceiver if (!noConnectivity) { // Datenverbindung vorhanden - Log.d("IBC", "connection established, starting service"); + Log.d("IBC", "connection established, (re-)starting service"); context.stopService(new Intent(context, SubscriptionService.class)); context .startService(new Intent(context, SubscriptionService.class)); diff --git a/src/de/mtbnews/android/service/SubscriptionService.java b/src/de/mtbnews/android/service/SubscriptionService.java @@ -56,7 +56,6 @@ public class SubscriptionService extends Service private SharedPreferences prefs; // Notification-Kategorien: - private static final int NOTIFICATION_ERROR = 1; private static final int NOTIFICATION_TOPIC = 2; private static final int NOTIFICATION_FORUM = 3; private static final int NOTIFICATION_MESSAGES = 4; @@ -88,10 +87,21 @@ public class SubscriptionService extends Service int intervalInMinutes = Integer.parseInt(prefs.getString( "subscription_service_interval", "180")); - Log.d(IBC.TAG, "Creating the timer"); - timer = new Timer(); - timer.scheduleAtFixedRate(new SubscriptionTask(), 2000, - intervalInMinutes * 60 * 1000); + // Prüfen, ob Service laufen soll und ein Benutzername vorhanden ist + if (prefs.getBoolean("autostart_subscription_service", false) + && !TextUtils.isEmpty(prefs.getString("username", ""))) + { + Log.d(IBC.TAG, "Creating the timer"); + timer = new Timer(); + timer.scheduleAtFixedRate(new SubscriptionTask(), 2000, + intervalInMinutes * 60 * 1000); + } + else + { + // Service soll nicht laufen, also sofort wieder stoppen + Log.d(IBC.TAG, "Stopping service"); + stopSelf(); + } } @@ -123,6 +133,7 @@ public class SubscriptionService extends Service try { + // Zuerst Login client.login(prefs.getString("username", ""), prefs.getString( "password", "")); @@ -221,18 +232,13 @@ public class SubscriptionService extends Service { // Kann vorkommen, wenn Login fehlschlägt oder Verbindung // abbricht - final Intent notificationIntent = new Intent( - SubscriptionService.this, IBCActivity.class); - final PendingIntent contentIntent = PendingIntent.getActivity( - SubscriptionService.this, 0, notificationIntent, 0); - - final Notification notification = createNotification(e - .getMessage(), R.string.error, null, getResources() - .getString(Utils.getResId(e.getErrorCode())), - contentIntent); - - notification.flags = Notification.FLAG_AUTO_CANCEL; - nm.notify(NOTIFICATION_ERROR, notification); + Log.w(IBC.TAG, e); + + // Ganz bewusst wird hier keine Fehlermeldung erzeugt: + // Oft kann es passieren, dass der Empfang schlecht wird und die + // Verbindung einen Timeout bekommt. In diesem Fall möchten wir + // den Benutzer aber nicht mit Fehlermeldungen nerven. Beim + // nächsten Timerevent wird der Abruf sowieso wieder probiert. } catch (Exception e) { @@ -251,7 +257,7 @@ public class SubscriptionService extends Service @Override public boolean cancel() { - Log.d(IBC.TAG, "Timer destroyed"); + Log.d(IBC.TAG, "Timer canceled"); return super.cancel(); } } @@ -267,7 +273,8 @@ public class SubscriptionService extends Service { Log.d(IBC.TAG, "Destroying service"); - timer.cancel(); // Alle Timer-Ereignisse stoppen. + if (timer != null) + timer.cancel(); // Alle Timer-Ereignisse stoppen. super.onDestroy(); }