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

サービスにバインドする必要があるアクティビティは次のとおりです。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));
    }   
}

onServiceConnected は呼び出されません。明らかな何かが欠けていますか?

4

3 に答える 3

2

startService()を使用しませんServiceConnectionbindService()します。

于 2010-03-20T20:22:33.857 に答える
0

onServiceConnectedは呼び出されません。

サービスとバインドするには、bindService()を呼び出す必要があります。持続的接続を提供し、接続の確立後にonServiceConnected()が呼び出されます。

2番目のポイント:-AIDL IPCメカニズムを使用している場合は、2つのdiffプロセス/アプリケーション間の通信が必要だと思います。ここでは、同じパッケージのサービス側とアクティビティ側の両方に.aidlファイルの同じコピーが必要です。次に、アクティビティ側で少し変更する必要があります。

あなたはここで見つけることができます

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

于 2012-01-18T05:19:24.830 に答える
0
Intent intent = new Intent(CallAlert.class.getName());
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
于 2010-06-19T13:11:50.107 に答える