【发布时间】:2015-03-28 00:42:38
【问题描述】:
我是 Android 编程新手,对 Java 不是很熟悉。
我在活动中加载带有 webview 的 url,然后调用 asynctask 来解析来自 url 的文本。解析后,我想将其发布到状态栏并发出通知。在 doInBackground 之后,我将解析后的字符串传递给onPostExecute,但出现编译器错误。
- 不确定是否可以从以下位置调用 notificationcompat.builder onPostExecute 在 asynctask 中,或者如果需要从 活动。
- 在 doInBackground 之后会自动调用 onPostExecute 吗?
谢谢,克雷格
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());
}
【问题讨论】:
标签: java android android-asynctask