Android : Can a notification to the status bar be called from an asynctask

on Friday, March 27, 2015


I am new to Android programming and not very experienced in Java. I am loading a url with a webview in an activity, then calling an asynctask to parse text from a url. After parsing I would like to post this to the status bar with a notification. After doInBackground I am passing the parsed string to onPostExecute but have compiler errors.

1- Not sure if I can call a notificationcompat.builder from onPostExecute in asynctask or if it needs to be called from an activity. 2- Will onPostExecute automatically get called after doInBackground? Thanks, Craig



protected void onPostExecute(String mycontent) {
// set up for notification in the notification status bar
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getApplicationContext())

.setSmallIcon(R.drawable.ic_stat_notify_lightning)
.setContentTitle("My Title Here")
.setContentText(mycontent);


// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, WebViewActivity.class);

// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(WebViewActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);

// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}

0 comments:

Post a Comment