use of android.content.ServiceConnection in project cornerstone by Onskreen.
the class PhoneWindowManager method takeScreenshot.
// Assume this is called from the Handler thread.
private void takeScreenshot() {
synchronized (mScreenshotLock) {
if (mScreenshotConnection != null) {
return;
}
ComponentName cn = new ComponentName("com.android.systemui", "com.android.systemui.screenshot.TakeScreenshotService");
Intent intent = new Intent();
intent.setComponent(cn);
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
synchronized (mScreenshotLock) {
if (mScreenshotConnection != this) {
return;
}
Messenger messenger = new Messenger(service);
Message msg = Message.obtain(null, 1);
final ServiceConnection myConn = this;
Handler h = new Handler(mHandler.getLooper()) {
@Override
public void handleMessage(Message msg) {
synchronized (mScreenshotLock) {
if (mScreenshotConnection == myConn) {
mContext.unbindService(mScreenshotConnection);
mScreenshotConnection = null;
mHandler.removeCallbacks(mScreenshotTimeout);
}
}
}
};
msg.replyTo = new Messenger(h);
msg.arg1 = msg.arg2 = 0;
if (mStatusBar != null && mStatusBar.isVisibleLw())
msg.arg1 = 1;
if (mNavigationBar != null && mNavigationBar.isVisibleLw())
msg.arg2 = 1;
try {
messenger.send(msg);
} catch (RemoteException e) {
}
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
if (mContext.bindService(intent, conn, Context.BIND_AUTO_CREATE)) {
mScreenshotConnection = conn;
mHandler.postDelayed(mScreenshotTimeout, 10000);
}
}
}
use of android.content.ServiceConnection in project DroidPlugin by DroidPluginTeam.
the class PluginManager method onServiceDisconnected.
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.i(TAG, "onServiceDisconnected disconnected!");
mPluginManager = null;
Iterator<WeakReference<ServiceConnection>> iterator = sServiceConnection.iterator();
while (iterator.hasNext()) {
WeakReference<ServiceConnection> wsc = iterator.next();
ServiceConnection sc = wsc != null ? wsc.get() : null;
if (sc != null) {
sc.onServiceDisconnected(componentName);
} else {
iterator.remove();
}
}
//服务连接断开,需要重新连接。
connectToService();
}
use of android.content.ServiceConnection in project AndroidChromium by JackyAndroid.
the class ClientManager method keepAliveForSession.
/** Tries to bind to a client to keep it alive, and returns true for success. */
public synchronized boolean keepAliveForSession(CustomTabsSessionToken session, Intent intent) {
// service (no calls to this service are made).
if (intent == null || intent.getComponent() == null)
return false;
SessionParams params = mSessionParams.get(session);
if (params == null)
return false;
String packageName = intent.getComponent().getPackageName();
PackageManager pm = mContext.getApplicationContext().getPackageManager();
// Only binds to the application associated to this session.
if (!Arrays.asList(pm.getPackagesForUid(params.uid)).contains(packageName))
return false;
Intent serviceIntent = new Intent().setComponent(intent.getComponent());
// This ServiceConnection doesn't handle disconnects. This is on
// purpose, as it occurs when the remote process has died. Since the
// only use of this connection is to keep the application alive,
// re-connecting would just re-create the process, but the application
// state has been lost at that point, the callbacks invalidated, etc.
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
boolean ok;
try {
ok = mContext.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE);
} catch (SecurityException e) {
return false;
}
if (ok)
params.setKeepAliveConnection(connection);
return ok;
}
use of android.content.ServiceConnection in project wifikeyboard by IvanVolosyuk.
the class WiFiKeyboard method onResume.
@Override
public void onResume() {
super.onResume();
// // FIXME: block default port out of use
// ServerSocketChannel ch;
// try {
// ch = ServerSocketChannel.open();
// ch.socket().setReuseAddress(true);
// ch.socket().bind(new java.net.InetSocketAddress(7777));
// ch = ServerSocketChannel.open();
// ch.socket().setReuseAddress(true);
// ch.socket().bind(new java.net.InetSocketAddress(1111));
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
serviceConnection = new ServiceConnection() {
//@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Debug.d("WiFiInputMethod connected to HttpService.");
try {
Stub listener = new PortUpdateListener.Stub() {
@Override
public void portUpdated(int newPort) throws RemoteException {
if (newPort != port) {
port = newPort;
setContentView(createView());
}
}
};
RemoteKeyboard.Stub.asInterface(service).setPortUpdateListener(listener);
} catch (RemoteException e) {
throw new RuntimeException("WiFiInputMethod failed to connected to HttpService.", e);
}
}
//@Override
public void onServiceDisconnected(ComponentName name) {
Debug.d("WiFiInputMethod disconnected from HttpService.");
}
};
if (this.bindService(new Intent(this, HttpService.class), serviceConnection, BIND_AUTO_CREATE) == false) {
throw new RuntimeException("failed to connect to HttpService");
}
setContentView(createView());
}
use of android.content.ServiceConnection in project wifikeyboard by IvanVolosyuk.
the class WiFiInputMethod method onCreate.
@Override
public void onCreate() {
super.onCreate();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "wifikeyboard");
// Debug.d("WiFiInputMethod started");
serviceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
// Debug.d("WiFiInputMethod connected to HttpService.");
try {
remoteKeyboard = RemoteKeyboard.Stub.asInterface(service);
keyboardListener = new RemoteKeyListener.Stub() {
@Override
public void keyEvent(int code, boolean pressed) throws RemoteException {
// Debug.d("got key in WiFiInputMethod");
receivedKey(code, pressed);
}
@Override
public void charEvent(int code) throws RemoteException {
// Debug.d("got key in WiFiInputMethod");
receivedChar(code);
}
@Override
public boolean setText(String text) throws RemoteException {
return WiFiInputMethod.this.setText(text);
}
@Override
public String getText() throws RemoteException {
return WiFiInputMethod.this.getText();
}
};
RemoteKeyboard.Stub.asInterface(service).registerKeyListener(keyboardListener);
} catch (RemoteException e) {
throw new RuntimeException("WiFiInputMethod failed to connected to HttpService.", e);
}
}
//@Override
public void onServiceDisconnected(ComponentName name) {
// Debug.d("WiFiInputMethod disconnected from HttpService.");
}
};
if (this.bindService(new Intent(this, HttpService.class), serviceConnection, BIND_AUTO_CREATE) == false) {
throw new RuntimeException("failed to connect to HttpService");
}
}
Aggregations