Impossibile associare al servizio
Domanda
Ho un file AIDL definito come segue:
package com.erbedo.callalert;
interface RemoteCallAlert {
void notifyCallEnded();
}
Il servizio è:
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 l'attività che ha di legarsi al servizio è: pacchetto 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));
}
}
L'onServiceConnected non viene chiamato. Mi sto perdendo qualcosa di ovvio?
Soluzione
startService()
non utilizza un ServiceConnection
. bindService()
fa.
Altri suggerimenti
Intent intent = new Intent(CallAlert.class.getName());
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
L'onServiceConnected non viene chiamato.
Per il legame con il servizio è necessario chiamare bindService (). Esso fornisce la connessione persistenza, e dopo la realizzazione del collegamento onServiceConnected () è chiamato.
Secondo punto: - Se si utilizza il meccanismo IPC AIDL allora penso che è necessario la comunicazione tra 2 diff processi / Application. Qui è necessario avere stessa copia del file di .aidl in entrambi i lati di servizio e attività in stesso pacchetto. Quindi è necessario modificare po 'nel vostro lato attività ..
si possono trovare qui