从这个常规Java文件开始,我启动了绑定服务(绑定服务是将对象插入SQL Lite DB。它有一个内容提供者和一个数据库服务)。我已经验证了db和表是在create上创建的。
问题:当我要通过服务将常规Java类(ParseString.java)文件中的对象插入Db时,我发现mBound始终为false而我插入记录时无法绑定到服务。基本上onServiceConnected永远不会被调用!!请你能救我吗?
MyAsyncTask.java
// post post方法 protected void onPostExecute(String finish){
pString = new ParseString(context);
// Inserting the data
pString.insertItems("Class Room 1", "Kid 1", "2", "34.99", "Sunshine Elementary", "3", 1);
}
// ParseString Java绑定绑定服务
public class ParseString {
//This service connects and manages the DB operations
ParseService mService;
boolean mBound = false;
public ParseString(Context c) {
context=c;
Intent intent = new Intent(context, ParseService.class);
context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
public void insertItems(String classNo, String studentName, String sectionCount, String studentSpend, String schoolName, String rank, int ID) {
Log.v("Mbound ->", "is"+mBound);
// mBound is always false.Why??
if (mBound) {
mService.addTask(classNo,studentName,sectionCount,studentSpend,schoolName,rank,ID );
}
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,IBinder service) {
AddBinder binder = (AddBinder) service;
mService = binder.getService();
Log.v("Setting ->","true");
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
}