【问题标题】:Displaying Notification after Background Process done Android后台进程完成后显示通知Android
【发布时间】:2013-01-19 01:17:54
【问题描述】:

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

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

提前致谢。

【问题讨论】:

    标签: java android multithreading http-request loopj


    【解决方案1】:

    您可以使用Handler 或致电Activity.runOnUiThread() 来完成。所以你要么将HandlerActivity 对象传递给你的静态方法,然后在你的onSuccess() 方法中,做,

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

    或者,

    handler.post(new Runnable() {
        @Override
        public void run() {
          // i'm on the UI thread!
        }
      }
    );
    

    【讨论】:

      【解决方案2】:

      我猜你的意思是作为后台进程的服务。 Service 有很多内置方法,如onCreateonStartCommandonDestroy 等。我建议使用通知,因为通知不需要 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);
      }
      

      【讨论】:

        【解决方案3】:

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

        在课堂上进行更新:

        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/创建 UI 的上下文,这仍然有效。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-22
          • 1970-01-01
          • 1970-01-01
          • 2015-11-08
          相关资源
          最近更新 更多