代码之家  ›  专栏  ›  技术社区  ›  lbedogni

无法绑定到服务

  •  0
  • lbedogni  · 技术社区  · 16 年前

    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。我是不是漏掉了什么明显的东西?

    3 回复  |  直到 16 年前
        1
  •  2
  •   CommonsWare    16 年前

    startService() 不使用 ServiceConnection . bindService() 做。

        2
  •  0
  •   xenonite    16 年前
    Intent intent = new Intent(CallAlert.class.getName());
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    
        3
  •  0
  •   Rahul Kushwaha    14 年前

    未调用onServiceConnected。

    要与服务绑定,需要调用bindService()。它提供持久性连接,连接建立后调用onServiceConnection()。

    第二点:- 如果您使用的是AIDL IPC机制,那么我认为您需要两个不同进程/应用程序之间的通信。在这里,您需要在同一个包的服务端和活动端拥有相同的.aidl文件副本。然后你需要修改一下你的活动方面。。

    你可以在这里找到

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

    推荐文章