我正在使用 IntentService 在后台更改墙纸。它在收到推送通知时被调用。如果在应用程序打开时收到通知,下面的代码可以正常工作。但是如果应用程序被关闭/杀死(通过在菜单按钮的帮助下将其刷掉)则不起作用
@Override
protected void onHandleIntent(Intent intent) {
//Toast.makeText(this, "Intent", Toast.LENGTH_SHORT).show();
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
String imageUrl = intent.getExtras().getString("imageUrl");
try {
URL url;
try {
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(this);
url = new URL(
imageUrl);
Bitmap bmp = BitmapFactory.decodeStream(url
.openConnection().getInputStream());
myWallpaperManager.setBitmap(bmp);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
PushReceiver.completeWakefulIntent(intent);
}
打开应用程序时工作正常。关闭/杀死时,它无法在以下位置下载图像BitmapFactory.decodeStream(url.openConnection().getInputStream())
下面是堆栈跟踪。
我正在使用 Mi3 Xiaomi 设备(不确定这是否有帮助)
应用拥有 android.permission.INTERNET、android.permission.ACCESS_NETWORK_STATE 的权限
W/System.err: java.net.ConnectException: failed to connect to sgsbirds.com/182.50.149.129 (port 80): connect failed: ECONNREFUSED (Connection refused)
W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:114)
W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:460)
W/System.err: at java.net.Socket.connect(Socket.java:833)
W/System.err: at com.android.okhttp.internal.Platform.connectSocket(Platform.java:152)
W/System.err: at com.android.okhttp.Connection.connect(Connection.java:101)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
W/System.err: at org.dycusa.sgs.wallpapers.IntentServiceManager.onHandleIntent(IntentServiceManager.java:111)
W/System.err: at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:136)
W/System.err: at android.os.HandlerThread.run(HandlerThread.java:61)
W/System.err: Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
W/System.err: at libcore.io.Posix.connect(Native Method)
W/System.err: at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
W/System.err: at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:112)
W/System.err: ... 16 more