سؤال
لدي ملف 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.callert ؛
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 () يسمى.
النقطة الثانية:- إذا كنت تستخدم آلية IPC AIDL ، فأعتقد أنك بحاجة إلى الاتصال بين 2 من العمليات/التطبيق. يجب أن يكون لديك نفس النسخة من ملف .AIDL في كل من الخدمة ونشاط النشاط في نفس الحزمة. ثم تحتاج إلى تعديل القليل في جانب نشاطك ..
يمكنك أن تجد هنا
لا تنتمي إلى StackOverflow