我想知道是否可以从对话框启动服务。
不知何故,我的 bindService 函数给出了错误。
这是简短的代码。
清单文件:
<service android:name="MyService">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
这里是服务
public class MyService extends Service {
private final IBinder mBinder = new MainBinder();
public class MainBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
MyService() {
mContext = this;
Log.d(LOG_TAG,"Constructor ");
}
public void onCreate() {
Log.d(LOG_TAG,"entered onCreate ");
super.onCreate();
}
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return mBinder;
}
protected int OnStartCommand (Intent intent, int flags, int startId) {
Log.d(LOG_TAG, "start service");
return START_STICKY;
}
}
并从 Dialog 调用
public Dialog onCreateDialog(Bundle savedInstanceState) {
this.mContext = getActivity();
initService();
}
private void initService() {
boolean ret;
Intent i = new Intent(mContext, MyService.class);
//mContext.startService(i);
ret = mContext.bindService(i, myConnection, Context.BIND_AUTO_CREATE);
Log.d(LOG_TAG, "initService() bound " + ret);
}
private ServiceConnection myConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
MyService.MainBinder binder = (MyService.MainBinder) service;
mBoundService = binder.getService();
isBound = true;
Log.d(LOG_TAG,"Bound to Service");
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
isBound = false;
}
};
任何线索为什么我总是从 bindService() 返回为假?
当我尝试从对话框启动服务时,我的应用程序也会出现类似的错误
09-25 16:13:32.054: W/ResourceType(1220): Failure getting entry for 0x010802c9 (t=7 e=713) in package 0 (error -75)
我还尝试通过活动本身来启动服务。仍然遇到同样的问题。