Não posso vincular ao serviço
Pergunta
Eu tenho um arquivo AIDL definido da seguinte forma:
package com.erbedo.callalert;
interface RemoteCallAlert {
void notifyCallEnded();
}
O serviço é:
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
}
}
e a atividade que precisa se ligar ao serviço é: package 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));
}
}
O OnserviceConnected não é chamado. Estou perdendo algo óbvio?
Solução
startService()
não usa um ServiceConnection
. bindService()
faz.
Outras dicas
Intent intent = new Intent(CallAlert.class.getName());
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
O OnserviceConnected não é chamado.
Para vincular -se ao serviço, você precisa chamar BindService (). Ele fornece a conexão de persistência e, após o estabelecimento de conexão, oServiceConnect () é chamado.
Segundo ponto:- Se você estiver usando o mecanismo AIDL IPC, acho que precisa da comunicação entre 2 processos/aplicativos diff. Aqui você precisa ter a mesma cópia do arquivo .aidl no lado de serviço e atividade no mesmo pacote. Então você precisa modificar um pouco no lado da sua atividade.
Você pode encontrar aqui