用WallpaperService沟通 [英] Communicate with a WallpaperService
问题描述
有没有办法直接与通信的 WallpaperService
从活动
它看起来并不像我可以使用正常的业务通信类,因为 onBind
方法声明最后
在WallpaperService类我延长。值得一提的是,我指的是的我的的WallpaperService没有的任何的
Is there any way to directly communicate with a WallpaperService
from an Activity
? It doesn't look like I can use the normal service communication classes because the onBind
method is declared final
in the WallpaperService class I'm extending. Worth noting that I'm referring to my WallpaperService not any.
任何变通办法,如果这是不可能的?
Any workarounds if this isn't possible?
推荐答案
我的解决方案是使用当地的插座。我在墙纸的引擎
的构造函数创建一个 LocalServerSocket
的实例。这里有一个快速的实现。服务器上的一个单独的线程中运行,直接关系到 MyEngine
的生命周期。该线程将停止当 continueSocket
设置为假
。出现这种情况的onDestroy
。 问题是, LocalServerSocket.accept()
块,直到有事情做。的解决方法是将消息发送到我们自己的服务器,因此将通过循环再次运行和检查 continueSocket
(也就是现在的假
),关闭服务器。检查 closeSocketServer
方法。我有它在的onDestroy
在本例中运行,但你可能要在其他地方,比如 onSurfaceDestroyed
使用它,并添加自己的理智检查。
My solution was to use local sockets. I created an instance of a LocalServerSocket
in the constructor of my wallpaper's Engine
. Here's a quick implementation. Server runs on a separate thread and is directly tied to the lifecycle of MyEngine
. The thread will stop when continueSocket
is set to false
. This happens onDestroy
. Problem is that LocalServerSocket.accept()
blocks until there's something to do. The workaround is to send a message to our own server so it will run through the loop again and check continueSocket
(which is now false
), closing the server. Check the closeSocketServer
method. I have it running in onDestroy
in the example but you might want to use it elsewhere like onSurfaceDestroyed
and add your own sanity checks.
public class MyWallpaperService extends WallpaperService {
@Override
public Engine onCreateEngine() {
return new MyEngine();
}
private class MyEngine extends Engine {
private boolean continueSocket = true;
MyEngine() {
new Thread() {
@Override
public void run() {
try {
LocalServerSocket server = new LocalServerSocket("MyAddress");
Log.d("SERVER READY", "Server is ready.");
while(continueSocket) {
LocalSocket receiver = server.accept();
if(receiver != null) {
InputStream input = receiver.getInputStream();
byte[] data = IOUtils.toByteArray(input);
Log.d("GOT DATA", new String(data));
}
}
server.close();
} catch (IOException ex) {
Log.wtf("IOEXCEPTION", ex);
}
}
}.start();
}
@Override
public void onDestroy() {
closeSocketServer();
super.onDestroy();
}
private void closeSocketServer() {
continueSocket = false;
try {
LocalSocket socket = new LocalSocket();
socket.connect(new LocalSocketAddress("MyAddress"));
socket.getOutputStream().write(new byte[0]);
socket.getOutputStream().close();
socket.close();
} catch (IOException ex) {
//
}
}
}
}
在我的活动
它可以很简单,因为这...
And in my Activity
it can be as simple as this...
try {
LocalSocket sender = new LocalSocket();
sender.connect(new LocalSocketAddress("MyAddress"));
String data = "Hello world!";
Log.d("SENT DATA", data);
sender.getOutputStream().write(data.getBytes());
sender.getOutputStream().close();
sender.close();
} catch (IOException ex) {
Log.wtf("IOEXCEPTION", ex);
}
logcat的最终看起来是这样的:
Logcat ends up looking like this:
D/SERVER READY﹕ Server is ready. (when the wallpaper starts up)
D/SENT DATA﹕ Hello world! (when the activity sends data)
D/GOT DATA﹕ Hello world! (when the wallpaper gets the data)
这篇关于用WallpaperService沟通的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!