Java 后台处理完成后显示通知

Java 后台处理完成后显示通知,java,android,multithreading,http-request,loopj,Java,Android,Multithreading,Http Request,Loopj,我正在使用在后台加载HTTP请求,完成后,我希望显示一个“成功”通知(对话框、toast等) 我将代码放在一个单独的(非活动)类中,该类带有一个执行后台请求的静态方法。最后,响应在onSuccess方法下的AsyncHttpResponseHandler中。在这种方法中,我可以打印出响应,确认请求已通过,将数据保存到sd卡/共享首选项,但如何访问UI线程以显示通知 提前感谢。您可以使用处理程序,或者通过调用Activity.runOnUiThread()来执行此操作。因此,您要么将处理程序,要么

我正在使用在后台加载HTTP请求,完成后,我希望显示一个“成功”通知(对话框、toast等)

我将代码放在一个单独的(非活动)类中,该类带有一个执行后台请求的静态方法。最后,响应在onSuccess方法下的AsyncHttpResponseHandler中。在这种方法中,我可以打印出响应,确认请求已通过,将数据保存到sd卡/共享首选项,但如何访问UI线程以显示通知


提前感谢。

您可以使用
处理程序,或者通过调用
Activity.runOnUiThread()
来执行此操作。因此,您要么将
处理程序
,要么将
活动
对象传递给静态方法,然后在
onSuccess()
方法中

activity.runOnUiThread(new Runnable() {
    @Override
    public void run() {
      // i'm on the UI thread!
    }
  }
);
或者


您可以使用
处理程序
,或者通过调用
Activity.runOnUiThread()
来执行此操作。因此,您要么将
处理程序
,要么将
活动
对象传递给静态方法,然后在
onSuccess()
方法中

activity.runOnUiThread(new Runnable() {
    @Override
    public void run() {
      // i'm on the UI thread!
    }
  }
);
或者


你可以发射一个带有该信息的本地广播,并用接收器敬酒

在进行更新的类中执行此操作:

Intent intent = new Intent("ACTION_TOAST");
intent.putExtra("message", "Success!");
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
然后,在任何可能想了解更新的活动中,执行以下操作:

BroadcastReceiver receiver = new BroadcastReceiver() {      
    @Override
    public void onReceive(Context context, Intent intent) {
        if ("ACTION_TOAST".equals(intent.getAction()) {
            Toast.makeText(MyActivity.this, intent.getStringExtra("message"), 
                    Toast.LENGTH_SHORT).show();
        }
    }
}

@Override
protected void onStart() {
    super.onStart();
    LocalBroadcastManager.getInstance(this).registerReceiver(
            receiver, new IntentFilter("ACTION_TOAST"));
}

@Override
protected void onStop() {
    LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
}

您仍然需要将一个上下文传递到静态方法中,但即使该上下文是一个服务或其他无法显示toast/create UI的上下文,也可以这样做。

您可以使用消息触发本地广播,并使用接收器显示toast

在进行更新的类中执行此操作:

Intent intent = new Intent("ACTION_TOAST");
intent.putExtra("message", "Success!");
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
然后,在任何可能想了解更新的活动中,执行以下操作:

BroadcastReceiver receiver = new BroadcastReceiver() {      
    @Override
    public void onReceive(Context context, Intent intent) {
        if ("ACTION_TOAST".equals(intent.getAction()) {
            Toast.makeText(MyActivity.this, intent.getStringExtra("message"), 
                    Toast.LENGTH_SHORT).show();
        }
    }
}

@Override
protected void onStart() {
    super.onStart();
    LocalBroadcastManager.getInstance(this).registerReceiver(
            receiver, new IntentFilter("ACTION_TOAST"));
}

@Override
protected void onStop() {
    LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
}

您仍然需要将一个上下文传递到静态方法中,但即使该上下文是一个服务或其他无法显示Toasts/CreateUI的上下文,也可以这样做。

我猜您指的是作为后台进程的服务。服务有许多内置方法,如
onCreate
onStartCommand
onDestroy
,等等。我建议使用通知,因为通知不需要UI线程来完成这项工作

创建一个方法来生成通知,并在HTML读取结束后调用它

private static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_stat_gcm;
    long when = System.currentTimeMillis();

    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context, MainActivity.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, notification);
}

我猜你指的是作为后台进程的服务。服务有许多内置方法,如
onCreate
onStartCommand
onDestroy
,等等。我建议使用通知,因为通知不需要UI线程来完成这项工作

创建一个方法来生成通知,并在HTML读取结束后调用它

private static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_stat_gcm;
    long when = System.currentTimeMillis();

    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context, MainActivity.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, notification);
}