需要在这里挖掘集体的脑力。我正在尝试一个非常简单的BindService并与Messenger示例进行通信。由于我在Message.obj字段中传递消息对象,因此我在自己的Android库项目中定义了消息,以便在服务和客户端之间共享。我可以通过将消息库包含为depdendency来编译,但在运行时dalvikvm抱怨它无法找到类。我已经阅读了有关出口和订单的其他相关问题,但同样的解决方案对我的情况没有帮助。这肯定是一个非常简单的错误,但我现在正在把头发拉出来。
应用程序取决于SupportServiceMessageLibrary。
App在项目的顺序选项卡中的src之前有库。
在App中未检查Lib导出,因为没有人正在使用App
图书馆正在导出其src和gen文件夹。
来自log cat的错误消息:
01-31 01:53:41.134: E/dalvikvm(22886): Could not find class 'com.example.service.RequestStatus', referenced from method com.example.MainActivity$ServiceReplyHandler.handleMessage
以下是代码片段
MainActivity
package com.example;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.Menu;
import android.widget.TextView;
import com.example.appserver.R;
import com.example.service.MessageType;
import com.example.service.RequestStatus;
public class MainActivity extends Activity {
private TextView serverStatus;
/** Messenger for sending message to service. */
Messenger mServiceMessenger = null;
/** Flag indicating whether we have called bind on the service. */
boolean mBound;
/** Messenger for receiving message from service */
Messenger mClientMessenger = null;
class ServiceReplyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
if (msg.what == MessageType.MSG_REPLY) {
RequestStatus reqStatus = (RequestStatus) msg.obj;
}
}
}
/**
* Class for interacting with the main interface of the service.
*/
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with the service has been
// established, giving us the object we can use to
// interact with the service. We are communicating with the
// service using a Messenger, so here we get a client-side
// representation of that from the raw IBinder object.
mServiceMessenger = new Messenger(service);
Message regMsg = Message.obtain(null,
MessageType.MSG_REGISTER);
regMsg.replyTo = mClientMessenger;
try {
mServiceMessenger.send(regMsg);
} catch (RemoteException e) {
}
mBound = true;
}
public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
mServiceMessenger = null;
mBound = false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_server);
serverStatus = (TextView) findViewById(R.id.server_status);
Intent startServiceIntent = new Intent();
startServiceIntent.setComponent(new ComponentName(
"com.example.supportservices",
"com.example.service.EntitlementService"));
bindService(startServiceIntent, mConnection, Context.BIND_AUTO_CREATE);
// Initialize messenger to receiver service messages;
mClientMessenger = new Messenger(new ServiceReplyHandler());
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onStart() {
super.onStart();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.server, menu);
return true;
}
}
信息本身
package com.example.service;
import org.json.JSONException;
import org.json.JSONObject;
import android.text.TextUtils;
public class RequestStatus {
public static int REQUEST_SUCCESS = 0;
public static int REQUEST_FAILURE = 1;
public static String TYPE = "request_status";
public static String STATUS_JSON_KEY = "status";
private int status;
public RequestStatus(int status) {
this.status = status;
}
public static RequestStatus fromJson(String json) {
try {
JSONObject msg = new JSONObject(json);
String type = msg.getString("type");
if (TextUtils.isEmpty(type) || TYPE.equals(type) == false) {
return null;
}
return(new RequestStatus(msg.getJSONObject("body").getInt(STATUS_JSON_KEY)));
} catch (JSONException e) {
return null;
}
}
public String toJson() {
JSONObject msg = new JSONObject();
try {
msg.put("type", TYPE);
JSONObject body = new JSONObject();
body.put(STATUS_JSON_KEY, status);
msg.put("body", body);
} catch (JSONException e) {
return null;
}
return msg.toString();
}
}
答案 0 :(得分:0)
转到App的项目属性,并将其添加为Android下的库以及构建路径下的项目依赖项。