No se puede enlazar al Servicio
Pregunta
Tengo un archivo AIDL define como sigue:
package com.erbedo.callalert;
interface RemoteCallAlert {
void notifyCallEnded();
}
El servicio es:
package com.erbedo.callalert;
public class CallAlert extends Service {
Filter callListener;
private final RemoteCallAlert.Stub mBinder = new RemoteCallAlert.Stub() {
@Override
public void notifyCallEnded() throws RemoteException {
// TODO
}
};
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "CallAlert Created", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "CallAlert Destroyed", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Toast.makeText(this, "CallAlert Started", Toast.LENGTH_LONG).show();
callListener = new Filter();
TelephonyManager tm =
(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(this.callListener, PhoneStateListener.LISTEN_CALL_STATE);
}
public void callEnded() {
// TODO
}
}
y de la actividad que ha de unirse al servicio es: paquete com.erbedo.callalert;
public class DummyStart extends Activity {
Filter callListener;
RemoteCallAlert mService;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
Log.d("CONNECT","OK");
}
public void onServiceDisconnected(ComponentName className) {
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout l = new LinearLayout(this);
setContentView(l);
this.startService(new Intent(this, CallAlert.class));
}
}
El onServiceConnected no se llama. Me estoy perdiendo algo obvio?
Solución
startService()
no utiliza un ServiceConnection
. bindService()
hace.
Otros consejos
Intent intent = new Intent(CallAlert.class.getName());
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
El onServiceConnected no se llama.
Para la unión con el servicio que necesita llamar bindService (). Se proporciona la conexión persistencia, y después de establecimiento de la conexión onServiceConnected () se llama.
Segundo punto: - Si está utilizando el mecanismo IPC AIDL entonces creo que necesita la comunicación entre 2 procesos de diferencias / Aplicación. Aquí es necesario tener misma copia del archivo .aidl en ambos lado del servicio y la actividad en un mismo envase. Luego hay que modificar poco en su lado Actividad ..
se puede encontrar aquí