mercredi 29 avril 2015

Setting the text in a notification to a random string

I'm trying to get the notification to pick from my a random string from my array to display every time it appears. I made "random" a random string from my array and put it in my "notification.setLatestEventInfo." Here is what i have so far

public class MyAlarmService extends Service
{
Random r;
private NotificationManager mManager;
String[] fruits = {"Apple","Mango","Peach","Banana","Orange","Grapes","Watermelon","Tomato"};;
int idx = r.nextInt(fruits.length);
String random = (fruits[idx]);




@Override
public IBinder onBind(Intent arg0)
{
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate()
{
    // TODO Auto-generated method stub
    super.onCreate();
}

@SuppressWarnings("static-access")
@Override
public void onStart(Intent intent, int startId)
{
    super.onStart(intent, startId);

    mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);

    Notification notification = new Notification(R.mipmap.ic_launcher,"Hey!!!", System.currentTimeMillis());
    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(this.getApplicationContext(), "My favorite fruit is the", random, pendingNotificationIntent);

    mManager.notify(0, notification);
}

@Override
public void onDestroy()
{
    // TODO Auto-generated method stub
    super.onDestroy();
}

}

All it does with this is force close.




Aucun commentaire:

Enregistrer un commentaire