我正在尝试使用ASL library捕获屏幕截图。
这是清单文件的一部分:
<service android:name=".services.ScreenshotService">
<intent-filter>
<action android:name="pl.polidea.asl.ScreenshotService.BIND" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
这是我活动的一部分:
private IScreenshotProvider aslProvider = null;
private ServiceConnection aslServiceConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d("service", "onServiceConnected called");
aslProvider = IScreenshotProvider.Stub.asInterface(service);
}
};
我通过onCreate
方法绑定服务,如下所示:
Intent intent2 = new Intent("pl.polidea.asl.ScreenshotService.BIND");
intent2.setClass(this, ScreenshotService.class);
bindService (intent2, aslServiceConn, Context.BIND_AUTO_CREATE);
这是我要捕获屏幕截图时单击按钮的代码:
public void captureScreenshotMethod(){
try {
if (aslProvider == null)
Toast.makeText(TrackerLogsActivity.this, "Prov = null", Toast.LENGTH_SHORT).show();
else if (!aslProvider.isAvailable())
Toast.makeText(TrackerLogsActivity.this, "Prov NA", Toast.LENGTH_SHORT).show();
else {
String file = aslProvider.takeScreenshot();
if (file == null)
Toast.makeText(TrackerLogsActivity.this, "Error in screenshot capture", Toast.LENGTH_SHORT).show();
else {
Toast.makeText(TrackerLogsActivity.this, "Screenshot captured & path - "+file, Toast.LENGTH_SHORT).show();
//Bitmap screen = BitmapFactory.decodeFile(file);
//imgScreen.setImageBitmap(screen);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我总是收到“ Prov NA”的吐司。尝试清洁+构建。尝试重新启动Android Studio。有什么帮助吗?我想念什么?
编辑:
深入研究问题,并调试ASL的ScreenshotService的方法,我到此为止:
我在ConnectException
行上收到sock.connect
:
private boolean isNativeRunning() {
try {
Socket sock = new Socket();
sock.connect(new InetSocketAddress("localhost", PORT), TIMEOUT); // short timeout
}
catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
Logcat输出:
12-06 16:50:42.484 21278-22065/com.pkg.myapp W/System.err: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:341)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
at java.net.Socket.connect(Socket.java:616)
at com.pkg.myapp.services.ScreenshotService.isNativeRunning(ScreenshotService.java:100)
at com.pkg.myapp.services.ScreenshotService.access$100(ScreenshotService.java:34)
有人可以帮忙吗?