Skip to content Skip to sidebar Skip to footer

Android: Placing A Random Item From A String Array In To Notifications

I want my app to be able to pick a random item from this string array: Quote 1 Quote 2

Solution 1:

First get the random value from the string array (like the link you mentioned):

String[] array = context.getResources().getStringArray(R.array.animals_array);
String randomStr = array[new Random().nextInt(array.length)];

Then use NotificationManager to show randomStr to user:

Stringns= Context.NOTIFICATION_SERVICE;
NotificationManagermNotificationManager= (NotificationManager) getSystemService(ns);

inticon= R.drawable.icon4;        
CharSequencetickerText="ticker-text";  
longwhen= System.currentTimeMillis();         
Contextcontext= getApplicationContext();     
CharSequencecontentTitle="MyTitle";  
CharSequencecontentText= randomStr;      
IntentnotificationIntent=newIntent(this, Example.class);
PendingIntentcontentIntent= PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notificationnotification=newNotification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

privatestaticfinalintHELLO_ID=1;
mNotificationManager.notify(HELLO_ID, notification);

Post a Comment for "Android: Placing A Random Item From A String Array In To Notifications"