Android 服务未从活动绑定,空指针异常
这是我的密码Android 服务未从活动绑定,空指针异常,android,android-service,Android,Android Service,这是我的密码 public class ScheduleClient { // The hook into our service private ScheduleService mBoundService; // The context to start the service in private Context mContext; // A flag if we are connected to the service or not
public class ScheduleClient {
// The hook into our service
private ScheduleService mBoundService;
// The context to start the service in
private Context mContext;
// A flag if we are connected to the service or not
private boolean mIsBound;
public ScheduleClient(Context context) {
mContext = context;
}
public void doBindService() {
// Establish a connection with our service
mContext.bindService(new Intent(mContext, ScheduleService.class),
mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with our service has been
// established,
// giving us the service object we can use to interact with our
// service.
mBoundService = ((ScheduleService.ServiceBinder) service)
.getService();
}
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
}
};
/**
* Tell our service to set an alarm for the given date
*
* @param c
* a date to set the notification for
*/
public void setAlarmForNotification(Calendar c) {
if (mBoundService != null)
mBoundService.setAlarm(c);
else
Log.e("@ScheduleClient", "mBoundService is null");
}
public void cancelAlarmForNotification() {
mBoundService.cancelAlarm();
}
/**
* When you have finished with the service call this method to stop it
* releasing your connection and resources
*/
public void doUnbindService() {
if (mIsBound) {
// Detach our existing connection.
mContext.unbindService(mConnection);
mIsBound = false;
}
}
}
清单文件
<service android:name="com.mobtecnica.inreez.reminder.ScheduleService" >
</service>
这里mContext.bindService(新的意图(mContext,ScheduleService.class),
mConnection,Context.BIND\u AUTO\u CREATE)代码>始终返回false。
因此,mBoundService
变为空。这意味着服务没有被绑定。我怎样才能解决这个问题。我正在尝试在我的应用程序中设置提醒。任何帮助都将不胜感激。那么,限制服务并因此初始化mBoundService
的方法是doBindService()
。这是从来没有被称为。看一看,有一个非常好的示例,它可以满足您的所有需求。是的,我调用了相同的scheduleClient=newscheduleclient(DashBoard.this);scheduleClient.doBindService()代码>