当前位置: 首页 > 知识库问答 >
问题:

使用Volley plus或OkHttp的Android更新通知服务进度

谷梁浩思
2023-03-14
mUBuilder.setContentTitle("Upload image")
    .setContentText("")
    .setAutoCancel(false)
    .setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT))
    .setSmallIcon(R.drawable.ic_launcher);
try {
    final File file = new File(IMAGE_PATH);
    final long totalSize = file.length();
    RequestBody requestBody = new MultipartBody.Builder()
        .addPart(Headers.of("Content-Disposition", "form-data; name=\"image\"; filename=\"" + file.getName() + "\""),
            new CountingFileRequestBody(file, "image/*", new CountingFileRequestBody.ProgressListener() {
            @Override
                                public void transferred(long num) {
                                    final float progress = (num / (float) totalSize) * 100;
                                    Log.d(TAG, "OUT THREAD: " + progress); //see in logs
                                    new Thread(
                                            new Runnable() {
                                                @Override
                                                public void run() {
                                                    Log.d(TAG, "IN THREAD: " + progress); //not visible in the logs
                                                    mUBuilder.setProgress(100,(int) progress, false);
                                                    mNotifyManager.notify(AppSettings.SEND_DATA_NOTIF, mUBuilder.build());
                                                }
                                            }
                                    );
                                }
                            }))
                    .build();

            Request request = new Request.Builder()
                    .url("http://posttestserver.com/post.php?dir=123")
                    .post(requestBody)
                    .build();

            client.newCall(request).enqueue(new Callback() {
                @Override public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                }

                @Override public void onResponse(Call call, Response response) throws IOException {
                    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

                    Headers responseHeaders = response.headers();
                    for (int i = 0, size = responseHeaders.size(); i < size; i++) {
                        System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
                    }
                    mUBuilder.setContentTitle("Upload complete!");
                    mNotifyManager.notify(AppSettings.SEND_DATA_NOTIF, mUBuilder.build());
                    System.out.println(response.body().string());
                }
            });
        }catch(Exception e){
            e.printStackTrace();
        }

凌空加码

mNotifyManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        notificationIntent = new Intent(getApplicationContext(), NMainActivity.class);
        final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext());
        mBuilder.setContentTitle("Upload image")
                .setContentText("")
                .setAutoCancel(false)
                .setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT))
                .setSmallIcon(R.drawable.ic_launcher);
        SimpleMultiPartRequest jsonRequest = new SimpleMultiPartRequest(Request.Method.POST, "http://posttestserver.com/post.php?dir=123",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.i(getClass().getName(), response);
                        mBuilder.setContentText("Upload image complete!");
                        mNotifyManager.notify(AppSettings.SEND_DATA_NOTIF, mBuilder.build());
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(getClass().getName(), error.toString());
            }
        }) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return mParams;
            }

        };
        jsonRequest.addFile("images", (IMAGE_PATH);
        jsonRequest.setFixedStreamingMode(true);
        jsonRequest.setShouldCache(false);
        jsonRequest.setShouldRetryServerErrors(true);
        jsonRequest.setOnProgressListener(new Response.ProgressListener() {
            @Override
            public void onProgress(long transferredBytes, long totalSize) {
                final int percentage = (int) ((transferredBytes / ((float) totalSize)) * 100);
                mBuilder.setProgress(100, percentage, false);
                mNotifyManager.notify(AppSettings.SEND_DATA_NOTIF, mBuilder.build());
//freeze system UI
            }
        });
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        mRequestQueue.add(jsonRequest);
        mRequestQueue.start();

共有1个答案

庄宇
2023-03-14

我找到了一个解决这个问题的简单方法

原文:https://stackoverflow.com/a/23107289/2127124

稍微修改一下OKHTTP的代码:

@Override
public void transferred(final int progress) {
    handler.post(new Runnable() {
        @Override
        public void run() {
            mUBuilder.setProgress(100, progress, true);
            mNotifyManager.notify(AppSettings.SEND_DATA_NOTIF, mUBuilder.build());
        }
    });
}
public class CountingFileRequestBody extends RequestBody {
    ...
    int latestPercentDone, percentDone;
    ...
    @Override
    public void writeTo(BufferedSink sink) throws IOException {
        ...
        while ((read = source.read(sink.buffer(), SEGMENT_SIZE)) != -1) {
            total += read;
            sink.flush();
            latestPercentDone = (int) ((total / (float) file.length()) * 100);
            if (percentDone != latestPercentDone) {
                percentDone = latestPercentDone;
                this.listener.transferred(percentDone);
            }
        }
    }
    public interface ProgressListener {
        void transferred(int num);
    }
}
jsonRequest.setOnProgressListener(new Response.ProgressListener() {
            int latestPercentDone, percentDone;
            @Override
            public void onProgress(long transferredBytes, long totalSize) {
                latestPercentDone = (int) ((transferredBytes / (float) totalSize) * 100);
                if (percentDone != latestPercentDone) {
                    percentDone = latestPercentDone;
                    mUBuilder.setProgress(100, percentDone, false);
                    mNotifyManager.notify(AppSettings.SEND_DATA_NOTIF, mUBuilder.build());
                }
            }
        });

您还可以使用以下代码:

final File file = new File(event.getPath().getPath());
final long totalSize = file.length();

new AsyncTask<Void, Integer, String>() {
    @Override
    protected void onProgressUpdate(Integer... values) {
        mUBuilder.setProgress(100, values[0], false);
        mNotifyManager.notify(AppSettings.SEND_DATA_NOTIF, mUBuilder.build());
    }

    @Override
    protected String doInBackground(Void... voids) {
        try{
            final File file = new File(event.getPath().getPath());
            final long totalSize = file.length();

            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost("http://posttestserver.com/post.php?dir=123");
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

            FileBody fb = new FileBody(file);

            builder.addPart("file", fb);
            final HttpEntity yourEntity = builder.build();
            class ProgressiveEntity implements HttpEntity {
                @Override
                public void consumeContent() throws IOException {
                    yourEntity.consumeContent();
                }

                @Override
                public InputStream getContent() throws IOException,
                        IllegalStateException {
                    return yourEntity.getContent();
                }

                @Override
                public Header getContentEncoding() {
                    return yourEntity.getContentEncoding();
                }

                @Override
                public long getContentLength() {
                    return yourEntity.getContentLength();
                }

                @Override
                public Header getContentType() {
                    return yourEntity.getContentType();
                }

                @Override
                public boolean isChunked() {
                    return yourEntity.isChunked();
                }

                @Override
                public boolean isRepeatable() {
                    return yourEntity.isRepeatable();
                }

                @Override
                public boolean isStreaming() {
                    return yourEntity.isStreaming();
                } 

                @Override
                public void writeTo(OutputStream outstream) throws IOException {

                    class ProxyOutputStream extends FilterOutputStream {
                        public ProxyOutputStream(OutputStream proxy) {
                            super(proxy);
                        }

                        public void write(int idx) throws IOException {
                            out.write(idx);
                        }

                        public void write(byte[] bts) throws IOException {
                            out.write(bts);
                        }

                        public void write(byte[] bts, int st, int end) throws IOException {
                            out.write(bts, st, end);
                        }

                        public void flush() throws IOException {
                            out.flush();
                        }

                        public void close() throws IOException {
                            out.close();
                        }
                    } 

                    class ProgressiveOutputStream extends ProxyOutputStream {
                        long totalSent;
                        int latestPercentDone, percentDone;

                        public ProgressiveOutputStream(OutputStream proxy) {
                            super(proxy);
                            totalSent = 0;
                        }

                        public void write(byte[] bts, int st, int end) throws IOException {

                            totalSent += end;
                            latestPercentDone = (int) ((totalSent / (float) totalSize) * 100);
                            if (percentDone != latestPercentDone) {
                                percentDone = latestPercentDone;
                                publishProgress(percentDone);
                            }

                            out.write(bts, st, end);
                        }
                    }

                    yourEntity.writeTo(new ProgressiveOutputStream(outstream));
                }
            }

            ProgressiveEntity myEntity = new ProgressiveEntity();

            post.setEntity(myEntity);
            HttpResponse response = client.execute(post);
            Log.d(TAG, response.toString());

        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

}.execute();

如果使用上一个示例代码,请不要忘记添加:

android {
    useLibrary 'org.apache.http.legacy'
}
dependencies {
    ...
    compile('org.apache.httpcomponents:httpcore:+') {
        exclude module: "httpclient"
    }
    compile('org.apache.httpcomponents:httpmime:4.3.6') {
        exclude module: "httpclient"
    }
    ...
}

在Android6.0.1上测试。

 类似资料:
  • 问题内容: 我有一个WCF服务,当数据库发生更改时(SQL Server 2005),它需要通知其客户端。只要我找到一种将任何更改通知我的 服务 的方法,这相对容易实现。我可能可以在表上创建数据库触发器,并让该触发器启动一个通知我的服务的小型服务客户端,但是我想知道是否有更好的方法可以做到这一点?让服务轮询数据库中的更改将是一个可行的解决方案,但是我不确定采用哪种最佳方法(并且最好向我的服务发送通

  • 现在每个应用程序都必须处理实体的更新功能。而不是在n个应用程序模块中实现更新功能。我想在Spring启动中构建一个通用接口或服务器。 我的问题是如何设计可用于上述场景的服务/接口。任何api或工具,可以帮助我实现这一点。我不想在每个应用程序模块中编写更新代码。 提前感谢。

  • 首先,我知道在后台或服务中已经有很多关于这个语音识别的问题。我想我已经在2周内检查了所有这些问题:P。但我不明白所有这些答案。我也使用了那里的代码,但它不起作用。我想要的是当用户点击按钮启动语音识别服务时,服务就会启动,甚至android也被锁定,服务会听取用户的指示。有人能告诉我如何实现这个或任何教程吗?我从2周开始就在做这个。我在谷歌和SO上搜索了很多。 ===================

  • 在RoomDatabase中创建数据库时,一切工作都很好,并且数据已成功加载,但是,我需要更新服务中生成的数据,问题是它需要下一行中的活动上下文: Dao接口: 存储库: 服务: 我希望服务在LiveData列表中执行、保存和刷新新结果,但我不能声明LiveData列表。

  • 我在使用NotificationListener服务时遇到两个问题 我在应用程序中使用了NotificationListenerService类。此侦听器#onNotificationPost方法接收所有通知。 我的代码如下所示 如何在通知访问对话框中更改上述消息?

  • 我需要每1分钟和每5米位移更新一次熔合位置(我知道这是一个错误的做法,但为了测试和在日志中显示),并且我在按下按钮后立即启动服务,我在logcat中获得位置,但只有一次。根据服务级别,onLocationChanged应该每1分钟调用一次,但它不会再次调用,即使我的GPS几乎每分钟打开一次,然后在之后关闭,但Logcat中仍然没有位置更新。我需要服务类来保持更新位置,而不影响UI或主线程 这是只显