【发布时间】:2017-12-20 15:35:01
【问题描述】:
我有一个 GcmListenerService,我知道每次发送消息时都会收到消息,因为我将图像的名称记录到 SQLite 数据库中,然后应该将其下载到同一个 onMessageRecieved 处理程序中。
它只在大约 75% 的时间下载图像,但我无法弄清楚它为什么会失败的押韵或原因。我什至设置了一个计数器,并且在放弃之前尝试下载图像 5 次。同一个应用程序在 10 种不同的设备上运行(其中一些与其他设备完全相同),它在某些设备上运行,而不是在其他设备上运行,同样没有明显的故障原因。
我最好的猜测是,当它失败时,它失败是因为设备处于“睡眠状态”或其他什么东西,并且仍然可以接收推送通知,但在该状态下无法访问网站?这是一个纯粹的假设,但我想不出还有什么会导致它有时随机不起作用。
我已经包含了来自 GcmListenerService 的一个 sn-p 代码,这就是问题所在。
@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
【问题讨论】:
-
这里有什么想法或建议吗?检查logcat
标签: android inputstream gcmlistenerservice