Android bindservice总是返回false
我有一个在API 14中工作的项目。现在我要转到API21,所以我要做需要做的更改 这是一款使用位置跟踪路线的应用程序。我有一个服务,负责定位的东西。但当我尝试绑定到该服务时,它总是返回错误,我不知道为什么 下面是我的代码。我甚至不知道该怎么开始看这个。服务不具有约束力的原因有哪些 下面是我的代码: 服务连接类Android bindservice总是返回false,android,Android,我有一个在API 14中工作的项目。现在我要转到API21,所以我要做需要做的更改 这是一款使用位置跟踪路线的应用程序。我有一个服务,负责定位的东西。但当我尝试绑定到该服务时,它总是返回错误,我不知道为什么 下面是我的代码。我甚至不知道该怎么开始看这个。服务不具有约束力的原因有哪些 下面是我的代码: 服务连接类 private ServiceConnection mConnection = new ServiceConnection() { public void onServiceCo
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);
// Now that we have the service messenger, lets send our messenger
Message msg = Message.obtain(null, LOCATION_CHANGED, 0, 0);
msg.replyTo = mClientMessenger;
/*
* In case we would want to send extra data, we could use Bundles:
* Bundle b = new Bundle(); b.putString("key", "hello world");
* msg.setData(b);
*/
try {
mServiceMessenger.send(msg);
} catch (RemoteException e) {
e.printStackTrace();
}
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;
}
};
bindService方法调用-val始终为false
public boolean bindService() {
/*
* Note that this is an implicit Intent that must be defined in the
* Android Manifest.
*/
Intent i = new Intent();
i.setPackage("com.example.conor.routetracker.ACTION_BIND");
boolean val = getBaseContext().getApplicationContext().bindService(i, mConnection,
Context.BIND_AUTO_CREATE);
return val;
}
Android清单
<?xml version="1.0" encoding="utf-8"?>
一行改变就可以节省时间 当我创造我的意图时,它应该是
Intent i = new Intent(getApplicationContext(), GPSService.class);
Intent i = new Intent(getApplicationContext(), GPSService.class);
public boolean bindService() {
Intent i = new Intent("com.example.conor.routetracker.ACTION_BIND");
i.setPackage("com.example.conor.routetracker")
return getBaseContext().getApplicationContext().bindService(i, mConnection, Context.BIND_AUTO_CREATE);
}