From fb554483c761666eb56753f9cd097b2e553949a2 Mon Sep 17 00:00:00 2001 From: gumartinm Date: Mon, 11 Jun 2012 07:22:06 +0200 Subject: [PATCH] No time for comments... --- Android/MobiAds/AndroidManifest.xml | 4 +++- .../src/de/android/mobiads/MobiAdsMainActivity.java | 20 +++++++++++++++++--- .../src/de/android/mobiads/MobiAdsService.java | 20 +++++++++++++------- .../src/de/android/mobiads/batch/MobiAdsBatch.java | 3 ++- .../de/android/mobiads/list/MobiAdsListActivity.java | 7 +------ .../android/mobiads/list/MobiAdsNewAdsActivity.java | 7 +------ 6 files changed, 37 insertions(+), 24 deletions(-) diff --git a/Android/MobiAds/AndroidManifest.xml b/Android/MobiAds/AndroidManifest.xml index f13b828..46df370 100644 --- a/Android/MobiAds/AndroidManifest.xml +++ b/Android/MobiAds/AndroidManifest.xml @@ -58,7 +58,9 @@ android:name=".list.MobiAdsNewAdsActivity" android:theme="@android:style/Theme.Black" android:screenOrientation="portrait" - android:configChanges="touchscreen|keyboard" > + android:configChanges="touchscreen|keyboard" + android:noHistory="true" + android:taskAffinity="de.android.mobiads.list"> diff --git a/Android/MobiAds/src/de/android/mobiads/MobiAdsMainActivity.java b/Android/MobiAds/src/de/android/mobiads/MobiAdsMainActivity.java index 064e0cc..856f093 100644 --- a/Android/MobiAds/src/de/android/mobiads/MobiAdsMainActivity.java +++ b/Android/MobiAds/src/de/android/mobiads/MobiAdsMainActivity.java @@ -1,6 +1,8 @@ package de.android.mobiads; import android.app.Activity; +import android.app.ActivityManager; +import android.app.ActivityManager.RunningServiceInfo; import android.content.ComponentName; import android.content.Context; import android.content.Intent; @@ -100,9 +102,11 @@ public class MobiAdsMainActivity extends Activity { public void onClickStartService(View v) { - Intent intent = new Intent(MobiAdsMainActivity.this, MobiAdsService.class); - intent.putExtra("cookie", MobiAdsMainActivity.cookie); - this.startService(intent); + if (!this.isMyServiceRunning()) { + Intent intent = new Intent(MobiAdsMainActivity.this, MobiAdsService.class); + intent.putExtra("cookie", MobiAdsMainActivity.cookie); + this.startService(intent); + } } public void onClickListLocalAds(View v) { @@ -111,4 +115,14 @@ public class MobiAdsMainActivity extends Activity { intent.putExtra("login", false); this.startActivity(intent); } + + private boolean isMyServiceRunning() { + ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); + for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { + if (MobiAdsService.class.getName().equals(service.service.getClassName())) { + return true; + } + } + return false; + } } diff --git a/Android/MobiAds/src/de/android/mobiads/MobiAdsService.java b/Android/MobiAds/src/de/android/mobiads/MobiAdsService.java index d326621..6364c64 100644 --- a/Android/MobiAds/src/de/android/mobiads/MobiAdsService.java +++ b/Android/MobiAds/src/de/android/mobiads/MobiAdsService.java @@ -120,11 +120,13 @@ public class MobiAdsService extends Service { CharSequence contentText; if ((noReadCount = this.mobiAdsBatch.noReadAdsCount()) == 0) { contentText = getText(R.string.remote_service_content_empty_notification); + showNotification(0, noReadCount, contentText, null); } else { contentText = getText(R.string.remote_service_content_notification); + showNotification(0, noReadCount, contentText, MobiAdsNewAdsActivity.class); } - showNotification(0, noReadCount, contentText); + return super.onStartCommand(intent, flags, startId); } @@ -153,12 +155,16 @@ public class MobiAdsService extends Service { /** * Show a notification while this service is running. */ - public void showNotification(final int level, final int noReadAds, CharSequence contentText) { - - Intent intent = new Intent(this, MobiAdsNewAdsActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); - // The PendingIntent to launch our activity if the user selects this notification - PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); + public void showNotification(final int level, final int noReadAds, CharSequence contentText, Class cls) { + PendingIntent contentIntent = null; + + if (cls != null) { + Intent intent = new Intent(this, MobiAdsNewAdsActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); + // The PendingIntent to launch our activity if the user selects this notification + contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); + } + // Set the icon, scrolling text and timestamp Notification.Builder notificationBuilder = new Notification.Builder(getApplicationContext()). diff --git a/Android/MobiAds/src/de/android/mobiads/batch/MobiAdsBatch.java b/Android/MobiAds/src/de/android/mobiads/batch/MobiAdsBatch.java index 06ff45a..4b59d62 100644 --- a/Android/MobiAds/src/de/android/mobiads/batch/MobiAdsBatch.java +++ b/Android/MobiAds/src/de/android/mobiads/batch/MobiAdsBatch.java @@ -33,6 +33,7 @@ import android.net.http.AndroidHttpClient; import android.util.Log; import de.android.mobiads.MobiAdsService; import de.android.mobiads.R; +import de.android.mobiads.list.MobiAdsNewAdsActivity; import de.android.mobiads.provider.Indexer; public class MobiAdsBatch { @@ -129,7 +130,7 @@ public class MobiAdsBatch { getText(R.string.remote_service_content_notification); } ((MobiAdsService)MobiAdsBatch.this.context). - showNotification(0, noReadCount, contentText); + showNotification(0, noReadCount, contentText, MobiAdsNewAdsActivity.class); } catch (Throwable e1) { //In case of any error, remove the index database and the file diff --git a/Android/MobiAds/src/de/android/mobiads/list/MobiAdsListActivity.java b/Android/MobiAds/src/de/android/mobiads/list/MobiAdsListActivity.java index 2879512..19d8447 100644 --- a/Android/MobiAds/src/de/android/mobiads/list/MobiAdsListActivity.java +++ b/Android/MobiAds/src/de/android/mobiads/list/MobiAdsListActivity.java @@ -162,10 +162,6 @@ public class MobiAdsListActivity extends Activity { public void showNotification(final int level, final int noReadAds, CharSequence contentText) { NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); - Intent intent = new Intent(this, MobiAdsNewAdsActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); - // The PendingIntent to launch our activity if the user selects this notification - PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); // Set the icon, scrolling text and timestamp Notification.Builder notificationBuilder = new Notification.Builder(getApplicationContext()). @@ -174,8 +170,7 @@ public class MobiAdsListActivity extends Activity { setWhen(System.currentTimeMillis()). setContentText(contentText). setContentTitle(getText(R.string.remote_service_title_notification)). - setNumber(noReadAds). - setContentIntent(contentIntent); + setNumber(noReadAds); Notification notification = notificationBuilder.getNotification(); notification.flags |= Notification.FLAG_NO_CLEAR; diff --git a/Android/MobiAds/src/de/android/mobiads/list/MobiAdsNewAdsActivity.java b/Android/MobiAds/src/de/android/mobiads/list/MobiAdsNewAdsActivity.java index fd42e16..9dcaff6 100644 --- a/Android/MobiAds/src/de/android/mobiads/list/MobiAdsNewAdsActivity.java +++ b/Android/MobiAds/src/de/android/mobiads/list/MobiAdsNewAdsActivity.java @@ -126,10 +126,6 @@ public class MobiAdsNewAdsActivity extends Activity { public void showNotification(final int level, final int noReadAds, CharSequence contentText) { NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); - Intent intent = new Intent(this, MobiAdsNewAdsActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); - // The PendingIntent to launch our activity if the user selects this notification - PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); // Set the icon, scrolling text and timestamp Notification.Builder notificationBuilder = new Notification.Builder(getApplicationContext()). @@ -138,8 +134,7 @@ public class MobiAdsNewAdsActivity extends Activity { setWhen(System.currentTimeMillis()). setContentText(contentText). setContentTitle(getText(R.string.remote_service_title_notification)). - setNumber(noReadAds). - setContentIntent(contentIntent); + setNumber(noReadAds); Notification notification = notificationBuilder.getNotification(); notification.flags |= Notification.FLAG_NO_CLEAR; -- 2.1.4