From: gumartinm Date: Sun, 6 May 2012 19:57:22 +0000 (+0200) Subject: Notification changes when an ad is received. X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=3a2cb372e0b36f590d9d5ec15713470d5fc65c9b;p=JavaForFun Notification changes when an ad is received. --- diff --git a/Android/MobiAds/gen/de/android/mobiads/R.java b/Android/MobiAds/gen/de/android/mobiads/R.java index e3278a0..cfb7c06 100644 --- a/Android/MobiAds/gen/de/android/mobiads/R.java +++ b/Android/MobiAds/gen/de/android/mobiads/R.java @@ -13,6 +13,7 @@ public final class R { public static final class drawable { public static final int alert_dialog_icon=0x7f020000; public static final int ic_launcher=0x7f020001; + public static final int wheelnotification=0x7f020002; } public static final class id { public static final int ads_entry_icon=0x7f05000f; diff --git a/Android/MobiAds/res/drawable-hdpi/ic_launcher.png b/Android/MobiAds/res/drawable-hdpi/ic_launcher.png index 8074c4c..96a442e 100644 Binary files a/Android/MobiAds/res/drawable-hdpi/ic_launcher.png and b/Android/MobiAds/res/drawable-hdpi/ic_launcher.png differ diff --git a/Android/MobiAds/res/drawable-ldpi/ic_launcher.png b/Android/MobiAds/res/drawable-ldpi/ic_launcher.png index 1095584..9923872 100644 Binary files a/Android/MobiAds/res/drawable-ldpi/ic_launcher.png and b/Android/MobiAds/res/drawable-ldpi/ic_launcher.png differ diff --git a/Android/MobiAds/res/drawable-mdpi/ic_launcher.png b/Android/MobiAds/res/drawable-mdpi/ic_launcher.png index a07c69f..359047d 100644 Binary files a/Android/MobiAds/res/drawable-mdpi/ic_launcher.png and b/Android/MobiAds/res/drawable-mdpi/ic_launcher.png differ diff --git a/Android/MobiAds/res/drawable-xhdpi/ic_launcher.png b/Android/MobiAds/res/drawable-xhdpi/ic_launcher.png new file mode 100644 index 0000000..71c6d76 Binary files /dev/null and b/Android/MobiAds/res/drawable-xhdpi/ic_launcher.png differ diff --git a/Android/MobiAds/res/drawable/wheelnotification.xml b/Android/MobiAds/res/drawable/wheelnotification.xml new file mode 100644 index 0000000..386a06f --- /dev/null +++ b/Android/MobiAds/res/drawable/wheelnotification.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/Android/MobiAds/src/de/android/mobiads/MobiAdsMainActivity.java b/Android/MobiAds/src/de/android/mobiads/MobiAdsMainActivity.java index 3fbeaa7..00dab75 100644 --- a/Android/MobiAds/src/de/android/mobiads/MobiAdsMainActivity.java +++ b/Android/MobiAds/src/de/android/mobiads/MobiAdsMainActivity.java @@ -1,6 +1,9 @@ package de.android.mobiads; import android.app.Activity; +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; import android.content.ComponentName; import android.content.Context; import android.content.Intent; @@ -14,6 +17,8 @@ import android.os.RemoteException; import android.view.View; public class MobiAdsMainActivity extends Activity { + /** For showing and hiding our notification. */ + private NotificationManager notificationManager; /** Messenger for communicating with service. */ Messenger mService = null; /** Flag indicating whether we have called bind on the service. */ @@ -22,7 +27,8 @@ public class MobiAdsMainActivity extends Activity { * Target we publish for clients to send messages to IncomingHandler. */ final Messenger mMessenger = new Messenger(new IncomingHandler()); - + /**TODO: I should use message with service to find out if the service is running instead of this booleanf field.*/ + private boolean isEnabledService; private static String cookie; @@ -55,6 +61,9 @@ public class MobiAdsMainActivity extends Activity { @Override public void onResume() { + if (this.isEnabledService) { + this.showNotification(0); + } super.onResume(); } @@ -136,12 +145,15 @@ public class MobiAdsMainActivity extends Activity { public void onClickStopService(View v) { this.stopService(new Intent(MobiAdsMainActivity.this, MobiAdsService.class)); + this.isEnabledService = false; } public void onClickStartService(View v) { Intent intent = new Intent(MobiAdsMainActivity.this, MobiAdsService.class); intent.putExtra("cookie", MobiAdsMainActivity.cookie); + this.notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); + this.isEnabledService = true; this.startService(intent); } @@ -150,4 +162,31 @@ public class MobiAdsMainActivity extends Activity { setComponent(new ComponentName("de.android.mobiads", "de.android.mobiads.list.MobiAdsListActivity")); this.startActivity(intent); } + + /** + * Show a notification while this service is running. + */ + public void showNotification(int level) { + + Intent intent = new Intent(this, MobiAdsMainActivity.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()). + setSmallIcon(R.drawable.wheelnotification, level). + setTicker(getText(R.string.remote_service_started)). + setWhen(System.currentTimeMillis()). + setContentText(getText(R.string.remote_service_started)). + setContentTitle(getText(R.string.remote_service_label)). + setContentIntent(contentIntent); + Notification notification = notificationBuilder.getNotification(); + notification.flags |= Notification.FLAG_NO_CLEAR; + + // Send the notification. + // We use a string id because it is a unique number. We use it later to cancel. + notificationManager.notify(R.string.remote_service_started, notification); + } } diff --git a/Android/MobiAds/src/de/android/mobiads/MobiAdsService.java b/Android/MobiAds/src/de/android/mobiads/MobiAdsService.java index 3e4f2fc..6004670 100644 --- a/Android/MobiAds/src/de/android/mobiads/MobiAdsService.java +++ b/Android/MobiAds/src/de/android/mobiads/MobiAdsService.java @@ -102,7 +102,7 @@ public class MobiAdsService extends Service { notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); // Display a notification about us starting. - showNotification(); + showNotification(0); return super.onStartCommand(intent, flags, startId); } @@ -132,12 +132,7 @@ public class MobiAdsService extends Service { /** * Show a notification while this service is running. */ - private void showNotification() { - // In this sample, we'll use the same text for the ticker and the expanded notification - CharSequence text = getText(R.string.remote_service_started); - - // Set the icon, scrolling text and timestamp - Notification notification = new Notification(R.drawable.ic_launcher, text, System.currentTimeMillis()); + public void showNotification(int level) { Intent intent = new Intent(this, MobiAdsMainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); @@ -145,17 +140,16 @@ public class MobiAdsService extends Service { PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); - Context context = getApplicationContext(); - - //notification.contentIntent = contentIntent; - // Set the info for the views that show in the notification panel. - //notification.defaults |= Notification.DEFAULT_VIBRATE; - //notification.ledARGB = 0xff00ff00; - //notification.ledOnMS = 300; - //notification.ledOffMS = 1000; - //notification.flags |= Notification.FLAG_SHOW_LIGHTS; + // Set the icon, scrolling text and timestamp + Notification.Builder notificationBuilder = new Notification.Builder(getApplicationContext()). + setSmallIcon(R.drawable.wheelnotification, level). + setTicker(getText(R.string.remote_service_started)). + setWhen(System.currentTimeMillis()). + setContentText(getText(R.string.remote_service_started)). + setContentTitle(getText(R.string.remote_service_label)). + setContentIntent(contentIntent); + Notification notification = notificationBuilder.getNotification(); notification.flags |= Notification.FLAG_NO_CLEAR; - notification.setLatestEventInfo(context, getText(R.string.remote_service_label), text, contentIntent); // Send the notification. // We use a string id because it is a unique number. We use it later to cancel. diff --git a/Android/MobiAds/src/de/android/mobiads/batch/MobiAdsBatch.java b/Android/MobiAds/src/de/android/mobiads/batch/MobiAdsBatch.java index 0268bec..8e2209e 100644 --- a/Android/MobiAds/src/de/android/mobiads/batch/MobiAdsBatch.java +++ b/Android/MobiAds/src/de/android/mobiads/batch/MobiAdsBatch.java @@ -31,6 +31,7 @@ import android.location.Location; import android.net.Uri; import android.net.http.AndroidHttpClient; import android.util.Log; +import de.android.mobiads.MobiAdsService; import de.android.mobiads.provider.Indexer; public class MobiAdsBatch { @@ -115,6 +116,7 @@ public class MobiAdsBatch { if ((uriInsert = updatedIndexer(objects)) != null) { try { downloadAds((String)objects.get("domain"), (String)objects.get("link"), (String) objects.get("id")); + ((MobiAdsService)MobiAdsBatch.this.context).showNotification(1); } catch (Throwable e1) { //In case of any error, remove the index database and the file //or chunk successfully stored before the error. diff --git a/Android/MobiAds/src/de/android/mobiads/list/MobiAdsListActivity.java b/Android/MobiAds/src/de/android/mobiads/list/MobiAdsListActivity.java index 41ca3d1..32ca262 100644 --- a/Android/MobiAds/src/de/android/mobiads/list/MobiAdsListActivity.java +++ b/Android/MobiAds/src/de/android/mobiads/list/MobiAdsListActivity.java @@ -6,9 +6,14 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import de.android.mobiads.MobiAdsMainActivity; import de.android.mobiads.R; import de.android.mobiads.provider.Indexer; import android.app.Activity; +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; @@ -45,8 +50,8 @@ public class MobiAdsListActivity extends Activity { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { Uri uri = Uri.parse(newsEntryAdapter.getItem(position).getTitle()); - startActivity( new Intent( Intent.ACTION_VIEW, uri ) ); - } + startActivity(new Intent(Intent.ACTION_VIEW, uri)); + } }); }