开发者

Can't bind to Service

I have an aidl file defined as follows:

package com.erbedo.callalert;

interface RemoteCallAlert {
    void notifyCallEnded();
}

The service is:

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();
    }

    @Overri开发者_JS百科de
    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
    }
}

and the Activity that has to bind to the service is: 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));
    }   
}

The onServiceConnected isn't called. Am I missing something obvious?


startService() does not use a ServiceConnection. bindService() does.


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


The onServiceConnected isn't called.

For binding with the service you need to call bindService(). It provide the persistence connection, and after connection establishment onServiceConnected() is called.

Second point:- If you are using AIDL IPC mechanism then I think you need the communication between 2 diff processes/Application. Here you need to have same copy of .aidl file in both service and Activity side in same PACKAGE. Then you need to modify little bit in your Activity side..

you can find here

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜