Sto tentando di associare un servizio, ma onBind()
restituisce sempre false.OnBind() sul servizio restituisce sempre False - Android
Questo è il codice per la 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;
}
};
questo è chiamata a bindService()
-
boolean test = getApplicationContext().bindService(new Intent(this, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);
Questa è la dichiarazione del servizio nel manifesto -
<service android:name=".Notifications.ScheduleService" android:enabled="true"/>
Ho letto domande precedenti riguardanti l'argomento e non sono riuscito a trovare ans wer (provato a cambiare il contesto di attività con il contesto dell'applicazione, ma non è stato di aiuto).
Sto usando utilizzando Frgaments e ActionBarSherlock e la mia attività si estende SlidingFragmentActivity
(Ecco perché sto usando il contesto dell'applicazione, che non aiuta).
Edit - Questo è il codice del servizio che sto cercando di iniziare -
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();
}
}
Qualsiasi aiuto sarà apprezzato. Grazie.
Penso che dovresti rimuovere le .Notifiche nel Manifest. –
Pubblica il codice di implementazione del servizio. – yorkw
Ho modificato la domanda con l'implementazione. – Tofira