2010-03-20 113 views
0

我有一個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未被調用。我錯過了明顯的東西嗎?

回答

2

startService()不使用ServiceConnectionbindService()呢。

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

的onServiceConnected不叫。

對於與服務綁定,您需要調用bindService()。它提供了持久連接,並且在連接建立onServiceConnected()被調用之後。

第二點: - 如果您使用的是AIDL IPC機制,那麼我認爲您需要2個diff進程/應用程序之間的通信。這裏你需要在同一個PACKAGE中的服務和Activity端都有相同的.aidl文件副本。然後,你需要修改點點在你的活動方..

,你可以在這裏找到

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

+1

爲什麼我需要將它們放在同一個包? 'Parcelable'是一個界面。你使用它的機制,而不是包的名稱。 – 2012-02-13 09:06:18