如何延迟函数调用?

啊。
public class NotificationReceivedCheckDelivery extends NotificationExtenderService {
   @Override
   protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
      OverrideSettings overrideSettings = new OverrideSettings();

      overrideSettings.extender = new NotificationCompat.Extender() {
         @Override
         public NotificationCompat.Builder extend(NotificationCompat.Builder builder) {
            // Sets the background notification color to Yellow on Android 5.0+ devices.
            return builder.setColor(new BigInteger("FFFFEC4F", 16).intValue());
         }
      };

      OSNotificationDisplayedResult displayedResult = displayNotification(overrideSettings);
    Log.d("ONES",receivedResult.payload.title);
      JSONObject AdditionalData = receivedResult.payload.additionalData;
        Log.d("Adata",AdditionalData.toString());

      String uuid= null;
      try{
        //   {"uuid":"adddd"}
          uuid = AdditionalData.getString("uuid");

      }
      catch (JSONException e){
          Log.e("Error JSON","UUID",e);
      }



                // Create Object and call AsyncTask execute Method
                new FetchNotificationData().execute(uuid);

      return true;
   }



private class FetchNotificationData extends AsyncTask<String,Void, String> {

        @Override
        protected String doInBackground(String... uuids) {
            // These two need to be declared outside the try/catch
            // so that they can be closed in the finally block.
            HttpURLConnection urlConnection = null;
            BufferedReader reader = null;

            // Will contain the raw JSON response as a string.
            String forecastJsonStr = null;

            try {

                URL url = new URL("http://test.com/AppDeliveryReport?uuid="+uuids[0]);


                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();

                // Read the input stream into a String
                InputStream inputStream = urlConnection.getInputStream();
                StringBuffer buffer = new StringBuffer();
                if (inputStream == null) {
                    // Nothing to do.
                    return null;
                }
                reader = new BufferedReader(new InputStreamReader(inputStream));

                String line;
                while ((line = reader.readLine()) != null) {
                    // Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
                    // But it does make debugging a *lot* easier if you print out the completed
                    // buffer for debugging.
                    buffer.append(line + "\n");
                }

                if (buffer.length() == 0) {
                    // Stream was empty.  No point in parsing.
                    return null;
                }
                forecastJsonStr = buffer.toString();
                return forecastJsonStr;
            } catch (IOException e) {
                Log.e("PlaceholderFragment", "Error ", e);
                // If the code didn't successfully get the weather data, there's no point in attemping
                // to parse it.
                return null;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (final IOException e) {
                        Log.e("PlaceholderFragment", "Error closing stream", e);
                    }
                }
            }

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Log.i("json", s);
        }
}
}

我想延迟随机秒调用 FetchNotificationData 函数。这是一个交付报告 url 请求功能。每当应用程序收到来自 onesignal 的通知时,它都会调用 url。我不想立即用巨大的请求炸毁服务器。所以我想用随机秒延迟调用,这样服务器就必须在给定的时间内处理几个调用。

阿里·艾哈迈德

您可以使用处理程序来延迟对函数的调用

  new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(Splash.this, "I will be called after 2 sec", 
                                                              Toast.LENGTH_SHORT).show();
                    //Call your Function here..
                }
            }, 2000); // 2000 = 2 sec

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章