我有一项活动,我在后台致电服务,对于AIDL来说我还是一个新手

我的代码如下:

活动中

//declaration
private Intent mIntentInvenueService;
private IInvenueService mIInvenueService;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
mIntentInvenueService = new Intent(this, InvenueService.class);
    startService(mIntentInvenueService);
}
private final ServiceConnection mServiceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        mIInvenueService = IInvenueService.Stub.asInterface(service);
        try {
            mIInvenueService.registerCallback(mCallback);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        mIInvenueService = null;
        try {
            mIInvenueService.unregisterCallback(mCallback);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
};

private final IInvenueServiceCallback.Stub mCallback = new IInvenueServiceCallback.Stub() {
    @Override
    public void onReceiveResponse() throws RemoteException {
        runOnUiThread(new Runnable() {
            public void run() {
                if (flagChanged){

                }
                else
                {
                    cAdapter.notifyDataSetChanged();
                }

            }
        });
    }
};
    public void onResume() {
    super.onResume();
    bindService(mIntentInvenueService, mServiceConnection, BIND_AUTO_CREATE);
}

@Override
public void onDestroy() {
    super.onDestroy();
    unbindService(mServiceConnection);
    unregisterReceiver(mBroadcastReceiver);
}


在我的服务上:

private final RemoteCallbackList<IInvenueServiceCallback> mCallbacks = new RemoteCallbackList<IInvenueServiceCallback>();
private boolean isUpdated = false;
@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

private final IInvenueService.Stub mBinder = new IInvenueService.Stub() {
    @Override
    public void startProcess() throws RemoteException {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
               Toast.makeText(......).show()// Toast Here
            }
        }, 60000, 60000);
    }



    @Override
    public void unregisterCallback(IInvenueServiceCallback callback)
            throws RemoteException {
        mCallbacks.unregister(callback);
    }

    @Override
    public void registerCallback(IInvenueServiceCallback callback)
            throws RemoteException {
        mCallbacks.register(callback);

    }

};


我的AIDL:

interface IInvenueService {
void startProcess();
void registerCallback(IInvenueServiceCallback callback);
void unregisterCallback(IInvenueServiceCallback callback);
 }


我的问题是为什么吐司不显示?我是否缺少启动过程无法正常工作的内容?

有帮助吗?

最佳答案

您应该从UIThread显示Toasts,使用TimerTask将导致它在单独的线程上运行。

尝试使用Toast将可运行对象发布到处理程序。

关于java - Android AIDL StartProcess无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20115143/

10-10 23:59