从GcmListenerService下载文件仅在某些时候起作用

问题描述 投票:0回答:1

我有一个GcmListenerService,我知道每次发送都会收到消息,因为我将图像的名称记录到SQLite数据库中,该数据库应该在同一个onMessageRecieved处理程序中下载。

它只会在75%的时间内下载图像,但我无法弄清楚它为什么会失败的韵律或原因。我甚至设置了一个计数器,我试图在放弃之前下载图像5次。这个应用程序运行在10个不同的设备上(有些硬件与其他设备完全相同),它可以在某些设备上运行,而不是在其他设备上运行,因为故障没有明显的原因。

我最好的猜测是,当它失败时,它会失败,因为设备“睡着了”或某些东西仍然可以接收推送通知,但是无法联系到该状态的网站?这是一个纯粹的假设,但我想不出任何会导致这种情况随机无效的事情。

我已经包含了GcmListenerService的一段代码,这是问题所在。

    @Override
public void onMessageReceived(String from, Bundle data) {
    Gson gson = new Gson();
    String message = data.getString("message");
    PushNotificationMessageReceivedModel model = gson.fromJson(message, PushNotificationMessageReceivedModel.class);
    if (model.messageType.equals("getimage")) {
        DatabaseAdapter myDB = new DatabaseAdapter(getBaseContext());
        myDB.Open();
        myDB.InsertImage(model.messagePayload);
        myDB.Close();
        new AsyncTask<String, Void, String>() {
            @Override
            protected String doInBackground(String... params) {
                Bitmap mIcon11 = null;
                try {
                    InputStream in = new java.net.URL("http://www.mywebsite.com/UploadedImages/"+params[0]).openStream();
                    mIcon11 = BitmapFactory.decodeStream(in);
                } catch (Exception e) {
                    Log.e("Error", e.getMessage());
                }
                FileOutputStream out = null;
                try {
                    out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/MyApp/"+params[0]);
                    mIcon11.compress(Bitmap.CompressFormat.JPEG, 100, out);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (out != null) {
                            out.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return Environment.getExternalStorageDirectory() + "/MyApp/"+params[0];
            }
            @Override
            protected void onPostExecute(String path) {
                try {
                    Bitmap noteIcon = null;
                    noteIcon = Utilities.ResizeBySampleSize(path, 300, 300);
                    //notification stuff
                    Notification.Builder mBuilder;
                    NotificationManager mNotificationManager;
                    Notification notification;
                    mBuilder = new Notification.Builder(getBaseContext())
                            .setSmallIcon(R.drawable.ic_launcher)
                            .setContentTitle("something here")
                            .setContentText("something there")
                            .setOngoing(false)
                            .setAutoCancel(true)
                            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                            .setLargeIcon(noteIcon)
                            .setContentIntent(myPendingIntent);
                    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    notification = mBuilder.build();
                    mNotificationManager.notify(notificationNumber, notification);                        
                } catch (Exception e) {
                }
            }
        }.execute(model.messagePayload);
    }
}

有一件事需要注意,当它失败时,输出流仍然在SD卡上创建一个文件,它只是一个空文件。

这里有什么想法或建议吗?

TIA

android inputstream gcmlistenerservice
1个回答
1
投票

它可能由于各种原因而发生

  • Android Oreo has limitations在后台运行服务,因此您可能会在O设备上遇到此问题
  • Android Marshmallow上的Doze mode可以导致这一点,它将停止所有网络操作本身
  • GcmListenerService不是用于执行长时间运行的操作,你应该在其他一些工作服务中做很多事情,你可以在其上有更多的控制权

我建议你使用JobsSchedulersFirebase Dispatchers来完成这项任务,因为它会处理打盹模式,后台服务限制,没有网络场景等。

© www.soinside.com 2019 - 2024. All rights reserved.