是否可以从asynctask调用状态栏的通知

时间:2015-03-27 15:39:15

标签: java android android-asynctask

我是Android编程新手,在Java方面经验不足。

我在活动中加载带有webview的url,然后调用asynctask来解析url中的文本。解析后,我想通过通知将其发布到状态栏。在doInBackground之后,我将解析后的字符串传递给onPostExecute但是有编译器错误。

  1. 不确定我是否可以从中调用notificationcompat.builder asynctask中的onPostExecute或者是否需要从中调用 活性。
  2. 在doInBackground之后会自动调用onPostExecute吗?
  3. 谢谢,克雷格

    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());
    }
    

1 个答案:

答案 0 :(得分:0)

onPostExecute之后将在主线程上自动调用

doInBackground,你能告诉编译器错误的更多细节吗?