I'm trying to make an app which downloads (periodically) data from a server and notifies the user when downloaded.
I tried to use Toast message, but I need a notification in the status bar. I used some sample code from developer.android.com which notifies after clicking the button. But I need to send the notification without clicking the button.
The most precise question is
how to call the sendNotification after ".execute" in "run" function, which arguments should I use in sendNotification(?what here?) ?
This h.postDelayed
stuff is in OnCreate
method.
Here is the code:
h.postDelayed(new Runnable() {
public void run() {
new WebServiceHandler()
.execute("http://maciekb94.cba.pl/");
Toast.makeText(MainActivity.this, "Pobrano dane", Toast.LENGTH_SHORT).show();
//sendNotification();
h.postDelayed(this, delay);
}
},delay);
}
public void sendNotification(View view) {
Intent intent = new Intent(this,MainActivity.class);
//Uri.parse("http://developer.android.com/reference/android/app/Notification.html"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
// END_INCLUDE(build_action)
// BEGIN_INCLUDE (build_notification)
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_stat_new_message);
// Set the intent that will fire when the user taps the notification.
builder.setContentIntent(pendingIntent);
// Set the notification to auto-cancel. This means that the notification will disappear
// after the user taps it, rather than remaining until it's explicitly dismissed.
builder.setAutoCancel(true);
builder.setContentTitle("Pobrano dane");
builder.setContentText("Wejdź by je zobaczyć");
NotificationManager notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
//END_INCLUDE(send_notification)
}