use of android.content.ServiceConnection in project AsmackService by rtreffer.
the class SyncAdapter method bindService.
/**
* Bind to the xmpp transport service.
*/
private final synchronized void bindService() {
if (serviceConnection == null) {
XmppTransportService.start(applicationContext);
serviceConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
}
public void onServiceConnected(ComponentName name, IBinder binder) {
service = IXmppTransportService.Stub.asInterface(binder);
}
};
}
applicationContext.bindService(new Intent(IXmppTransportService.class.getName()), serviceConnection, Context.BIND_AUTO_CREATE);
}
use of android.content.ServiceConnection in project platform_frameworks_base by android.
the class AppWidgetServiceImpl method handleNotifyAppWidgetViewDataChanged.
private void handleNotifyAppWidgetViewDataChanged(Host host, IAppWidgetHost callbacks, int appWidgetId, int viewId, long requestTime) {
try {
callbacks.viewDataChanged(appWidgetId, viewId);
host.lastWidgetUpdateTime = requestTime;
} catch (RemoteException re) {
// It failed; remove the callback. No need to prune because
// we know that this host is still referenced by this instance.
callbacks = null;
}
// RemoteViewsFactory.onDataSetChanged() directly
synchronized (mLock) {
if (callbacks == null) {
host.callbacks = null;
Set<Pair<Integer, FilterComparison>> keys = mRemoteViewsServicesAppWidgets.keySet();
for (Pair<Integer, FilterComparison> key : keys) {
if (mRemoteViewsServicesAppWidgets.get(key).contains(appWidgetId)) {
final ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
IRemoteViewsFactory cb = IRemoteViewsFactory.Stub.asInterface(service);
try {
cb.onDataSetChangedAsync();
} catch (RemoteException e) {
Slog.e(TAG, "Error calling onDataSetChangedAsync()", e);
}
mContext.unbindService(this);
}
@Override
public void onServiceDisconnected(android.content.ComponentName name) {
// Do nothing
}
};
final int userId = UserHandle.getUserId(key.first);
Intent intent = key.second.getIntent();
// Bind to the service and call onDataSetChanged()
bindService(intent, connection, new UserHandle(userId));
}
}
}
}
}
use of android.content.ServiceConnection in project platform_frameworks_base by android.
the class AppWidgetServiceImpl method destroyRemoteViewsService.
// Destroys the cached factory on the RemoteViewsService's side related to the specified intent
private void destroyRemoteViewsService(final Intent intent, Widget widget) {
final ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
final IRemoteViewsFactory cb = IRemoteViewsFactory.Stub.asInterface(service);
try {
cb.onDestroy(intent);
} catch (RemoteException re) {
Slog.e(TAG, "Error calling remove view factory", re);
}
mContext.unbindService(this);
}
@Override
public void onServiceDisconnected(ComponentName name) {
// Do nothing
}
};
// Bind to the service and remove the static intent->factory mapping in the
// RemoteViewsService.
final long token = Binder.clearCallingIdentity();
try {
mContext.bindServiceAsUser(intent, conn, Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE, widget.provider.info.getProfile());
} finally {
Binder.restoreCallingIdentity(token);
}
}
use of android.content.ServiceConnection in project platform_frameworks_base by android.
the class LocalReceiver method onReceive.
public void onReceive(Context context, Intent intent) {
String resultString = LaunchpadActivity.RECEIVER_LOCAL;
if (BroadcastTest.BROADCAST_FAIL_REGISTER.equals(intent.getAction())) {
resultString = "Successfully registered, but expected it to fail";
try {
context.registerReceiver(this, new IntentFilter("foo.bar"));
context.unregisterReceiver(this);
} catch (ReceiverCallNotAllowedException e) {
//resultString = "This is the correct behavior but not yet implemented";
resultString = LaunchpadActivity.RECEIVER_LOCAL;
}
} else if (BroadcastTest.BROADCAST_FAIL_BIND.equals(intent.getAction())) {
resultString = "Successfully bound to service, but expected it to fail";
try {
ServiceConnection sc = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
}
public void onServiceDisconnected(ComponentName name) {
}
};
context.bindService(new Intent(context, LocalService.class), sc, 0);
context.unbindService(sc);
} catch (ReceiverCallNotAllowedException e) {
//resultString = "This is the correct behavior but not yet implemented";
resultString = LaunchpadActivity.RECEIVER_LOCAL;
}
} else if (LaunchpadActivity.BROADCAST_REPEAT.equals(intent.getAction())) {
Intent newIntent = new Intent(intent);
newIntent.setAction(LaunchpadActivity.BROADCAST_LOCAL);
context.sendOrderedBroadcast(newIntent, null);
}
try {
IBinder caller = intent.getIBinderExtra("caller");
Parcel data = Parcel.obtain();
data.writeInterfaceToken(LaunchpadActivity.LAUNCH);
data.writeString(resultString);
caller.transact(LaunchpadActivity.GOT_RECEIVE_TRANSACTION, data, null, 0);
data.recycle();
} catch (RemoteException ex) {
}
}
use of android.content.ServiceConnection in project platform_frameworks_base by android.
the class KeyChain method bindAsUser.
/**
* @hide
*/
@WorkerThread
public static KeyChainConnection bindAsUser(@NonNull Context context, UserHandle user) throws InterruptedException {
if (context == null) {
throw new NullPointerException("context == null");
}
ensureNotOnMainThread(context);
final BlockingQueue<IKeyChainService> q = new LinkedBlockingQueue<IKeyChainService>(1);
ServiceConnection keyChainServiceConnection = new ServiceConnection() {
volatile boolean mConnectedAtLeastOnce = false;
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
if (!mConnectedAtLeastOnce) {
mConnectedAtLeastOnce = true;
try {
q.put(IKeyChainService.Stub.asInterface(service));
} catch (InterruptedException e) {
// will never happen, since the queue starts with one available slot
}
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
Intent intent = new Intent(IKeyChainService.class.getName());
ComponentName comp = intent.resolveSystemService(context.getPackageManager(), 0);
intent.setComponent(comp);
if (comp == null || !context.bindServiceAsUser(intent, keyChainServiceConnection, Context.BIND_AUTO_CREATE, user)) {
throw new AssertionError("could not bind to KeyChainService");
}
return new KeyChainConnection(context, keyChainServiceConnection, q.take());
}
Aggregations