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

时间:2017-12-20 15:35:01

标签: android inputstream gcmlistenerservice

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

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

我最好的猜测是,当它失败时,它会失败,因为设备已经睡着了#34;或某些东西,仍然可以收到推送通知,但无法联系到该州的网站?这是一个纯粹的假设,但我无法想到任何可能导致其随机无效的事情。

我已经在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

1 个答案:

答案 0 :(得分:1)

可能由于各种原因

而发生
    关于在后台运行服务的
  • Android Oreo has limitations,因此您可能会在O设备上遇到此问题

  • 在Android Marshmallow上的
  • Doze mode可以导致此问题,它将自行停止所有网络操作

  • GcmListenerService不是为了进行长时间运行的操作,你应该在其他一些工作服务中做很多事情,你有更多的控制权

我建议您使用 JobsSchedulers Firebase Dispatchers 执行此任务,因为它会处理打盹模式,后台服务限制,没有网络场景等。