Вопрос

У меня есть файл aidl, определенный следующим образом:

package com.erbedo.callalert;

interface RemoteCallAlert {
    void notifyCallEnded();
}

Услуга заключается в:

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
    }
}

и действие, которое должно быть привязано к сервису, является:пакет 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));
    }   
}

onServiceConnected не вызывается.Я упускаю что-то очевидное?

Это было полезно?

Решение

startService() не использует ServiceConnection. bindService() делает.

Другие советы

Intent intent = new Intent(CallAlert.class.getName());
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

onServiceConnected не вызывается.

Для привязки к сервису вам необходимо вызвать bindService().Он обеспечивает постоянное соединение, и после установления соединения вызывается onServiceConnected() .

Второй момент:- Если вы используете механизм AIDL IPC, то я думаю, вам нужна связь между двумя различными процессами / приложением.Здесь вам нужно иметь одну и ту же копию файла .aidl как на стороне сервиса, так и на стороне активности в одном пакете.Затем вам нужно немного изменить свою деятельность..

вы можете найти здесь

http://www.zestofandroid.blogspot.com/

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top