我试图在MainActivity中绑定服务,绑定服务受在updateTheNotification()中定义的方法MainActivity中创建的意图的约束,如下所示:
public void updateTheNotification()
{
Intent intentz = new Intent(context.getApplicationContext(), NotificationService.class);
context.getApplicationContext().bindService(intentz, mConnection, Context.BIND_ABOVE_CLIENT);
if (mBound) {
// Call a method from the LocalService.
// However, if this call were something that might hang, then this request should
// occur in a separate thread to avoid slowing down the activity performance.
mService.changeTheUI(true);
Toast.makeText(this, "Service triggered", Toast.LENGTH_LONG).show();
}
}
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
NotificationService.LocalBinder binder = (NotificationService.LocalBinder) service;
mService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};updateTheNotification()由附加在通知按钮上的广播接收器onReceive方法调用。
发布于 2017-01-26 16:40:35
方法bindService()是异步的。这意味着该方法将立即返回,即使Service尚未绑定。
当Service绑定完成后,调用ServiceConnection的onServiceConnected()方法。由于此方法是在main (UI)线程上调用的,所以当代码在主(UI)线程上调用的任何其他方法(例如,onReceive() )中执行时,不能调用该方法。
您需要将您的处理分为两部分:
ServiceService后,继续处理https://stackoverflow.com/questions/41848256
复制相似问题