Tôi đang cố gắng ràng buộc một dịch vụ, nhưng onBind()
luôn trả về false.OnBind() trên dịch vụ luôn trả về False - Android
Đây là mã cho ServiceConnection
-
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;
}
};
này được gọi bindService()
-
boolean test = getApplicationContext().bindService(new Intent(this, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);
Đây là tuyên bố của dịch vụ trong Manifest -
<service android:name=".Notifications.ScheduleService" android:enabled="true"/>
Tôi đã đọc các câu hỏi trước về chủ đề và không thể tìm thấy ans wer (cố gắng chuyển ngữ cảnh Hoạt động với ngữ cảnh Ứng dụng, nhưng nó không giúp được).
Tôi đang sử dụng Frgaments và ActionBarSherlock và Hoạt động của tôi mở rộng SlidingFragmentActivity
(Đó là lý do tại sao tôi đang sử dụng ngữ cảnh ứng dụng, điều này không có tác dụng).
Edit - Đây là mã của dịch vụ tôi đang cố gắng để bắt đầu -
public class ScheduleService extends Service {
/**
* Class for clients to access
*/
public class ServiceBinder extends Binder {
public ScheduleService getService() {
return ScheduleService.this;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("ScheduleService", "Received start id " + startId + ": " + intent);
// We want this service to continue running until it is explicitly stopped, so return sticky.
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
// This is the object that receives interactions from clients. See
private final IBinder mBinder = new ServiceBinder();
/**
* Show an alarm for a certain date when the alarm is called it will pop up a notification
*/
public void setAlarm(Calendar c) {
// This starts a new thread to set the alarm
// You want to push off your tasks onto a new thread to free up the UI to carry on responding
new AlarmTask(this, c).run();
}
}
Bất kỳ trợ giúp sẽ được đánh giá cao. Cảm ơn.
Tôi nghĩ bạn nên xóa thông báo .Notifications trong tệp kê khai. –
Đăng mã triển khai dịch vụ của bạn. – yorkw
Tôi đã chỉnh sửa câu hỏi bằng cách triển khai. – Tofira