题
我有一个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机制那么我认为你需要2 DIFF流程/应用程序之间的通信。在这里,你需要有.aidl文件的同一个副本在同一个包既服务和活动的一面。然后,你需要修改点点在你的活动方..
您可以在这里找到。
不隶属于 StackOverflow