我正在编写一个简单的音乐播放器,并且创建了一个播放服务,它实现了一个 AIDL 接口来与客户端绑定,一个是简单的曲目浏览器,另一个是更简单的播放器活动。该服务控制 MediaPlayer 对象,而两个活动使用 ServiceConnections 来获取与该服务的连接。
这包含在两个活动的 onStart() 方法中:
@Override
public void onStart()
{
super.onStart();
Intent i = new Intent(this, PureService.class);
startService(i);
bindService(i, mConnection, 0);
}
Run Code Online (Sandbox Code Playgroud)
我这样做是为了让服务不会在解绑后立即停止。当然,这实际上并不是问题,因为我的活动根本拒绝与服务解除绑定。每当我的应用程序在这些活动中执行 unbindService 时,unbindService 每次都会抛出 IllegalArgumentException,无一例外(呵呵)。
在 onStop 方法中:
@Override
public void onStop()
{
super.onStop();
if (mBound) {
try {
unbindService(mConnection);
} catch (java.lang.IllegalArgumentException e)
{
//Print to log or make toast that it failed
}
}
mBound = false;
}
Run Code Online (Sandbox Code Playgroud)
我想知道的是:
提前致谢。
编辑:这是 …
这甚至可能吗?我有一个我已经用GPG制作的密钥对,但我找不到用这个密钥签名的方法.我真的不想用keytool或者其他任何东西制作新的密钥; 我宁愿使用我现在拥有的钥匙.谁知道我怎么能这样做?提前致谢.
我正在使用 Java 将输出写入 a PDDocument,然后将该文档附加到现有文档,然后再将其提供给客户端。
其中大部分运行良好。我只有一个小问题试图在写入时处理内容溢出PDDocument。我想跟踪文本插入文档的位置,以便当“光标”可以说超过某个点时,我将创建一个新页面,将其添加到文档中,创建一个新的内容流,并照常继续。
这是一些代码,显示了我想做的事情:
// big try block
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDPageContentStream content = new PDPageContentStream(doc, page);
int fontSize = 12;
content.beginText();
content.setFont(...);
content.moveTextPositionByAmount(margin, pageHeight-margin);
for ( each element in a collection of values ) {
content.moveTextPositionByAmount(0, -fontSize); // only moves down in document
// at this point, check if past the end of page, if so add a new page
if (content.getTextYPosition(...) < margin) { …Run Code Online (Sandbox Code Playgroud) 我需要为C语言编写一个用于Linux条形码阅读器的驱动程序.条形码阅读器通过串行总线工作.当我向条形码阅读器发送一组命令时,条形码阅读器应该向我返回状态消息.我设法配置端口并创建信号处理程序.在信号处理程序中,我读取了串行总线接收的数据.
所以问题是:我应该读取缓冲区中的数据然后使用它吗?我是否可以使用以这种方式配置的端口将数据写入缓冲区?当设备回复我时,根据该回复数据,我需要向设备发送另一个命令.另外,我可以write()用来写消息吗?如果我不能使用它,我应该使用什么命令?你可以用write命令帮我一点吗?
我发送给设备的命令总是7个字节,但是应答数据在7-32个字节之间变化.如果读取函数发送了不同数量的字节,那么我怎样才能确定收到所有数据,这样我才能使用它?
这是我写的一些代码.我正朝着正确的方向前进吗?再一次,这个想法非常简单:我正在向设备发送命令,设备会中断并回复.我读了发送的内容,我使用了回复数据,根据该数据,我发送了另一个命令.谢谢你.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <errno.h>
#include <termios.h>
void signal_handler_IO (int status); /* definition of signal handler */
int n;
int fd;
int connected;
char buffer[14];
int bytes;
struct termios termAttr;
struct sigaction saio;
int main(int argc, char *argv[])
{
fd = open("/dev/ttyUSB1", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
perror("open_port: Unable to open /dev/ttyO1\n");
exit(1);
} …Run Code Online (Sandbox Code Playgroud)