use of android.content.ServiceConnection in project JamsMusicPlayer by psaravan.
the class IabHelper method startSetup.
/**
* Starts the setup process. This will start up the setup process asynchronously.
* You will be notified through the listener when the setup process is complete.
* This method is safe to call from a UI thread.
*
* @param listener The listener to notify when the setup process is complete.
*/
public void startSetup(final OnIabSetupFinishedListener listener) {
// If already set up, can't do it again.
checkNotDisposed();
if (mSetupDone)
throw new IllegalStateException("IAB helper is already set up.");
// Connection to IAB service
logDebug("Starting in-app billing setup.");
mServiceConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
logDebug("Billing service disconnected.");
mService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
if (mDisposed)
return;
logDebug("Billing service connected.");
mService = IInAppBillingService.Stub.asInterface(service);
String packageName = mContext.getPackageName();
try {
logDebug("Checking for in-app billing 3 support.");
// check for in-app billing v3 support
int response = mService.isBillingSupported(3, packageName, ITEM_TYPE_INAPP);
if (response != BILLING_RESPONSE_RESULT_OK) {
if (listener != null)
listener.onIabSetupFinished(new IabResult(response, "Error checking for billing v3 support."));
// if in-app purchases aren't supported, neither are subscriptions.
mSubscriptionsSupported = false;
return;
}
logDebug("In-app billing version 3 supported for " + packageName);
// check for v3 subscriptions support
response = mService.isBillingSupported(3, packageName, ITEM_TYPE_SUBS);
if (response == BILLING_RESPONSE_RESULT_OK) {
logDebug("Subscriptions AVAILABLE.");
mSubscriptionsSupported = true;
} else {
logDebug("Subscriptions NOT AVAILABLE. Response: " + response);
}
mSetupDone = true;
} catch (RemoteException e) {
if (listener != null) {
listener.onIabSetupFinished(new IabResult(IABHELPER_REMOTE_EXCEPTION, "RemoteException while setting up in-app billing."));
}
e.printStackTrace();
return;
}
if (listener != null) {
listener.onIabSetupFinished(new IabResult(BILLING_RESPONSE_RESULT_OK, "Setup successful."));
}
}
};
Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
if (!mContext.getPackageManager().queryIntentServices(serviceIntent, 0).isEmpty()) {
// service available to handle that Intent
mContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
} else {
// no service available to handle that Intent
if (listener != null) {
listener.onIabSetupFinished(new IabResult(BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE, "Billing service unavailable on device."));
}
}
}
use of android.content.ServiceConnection in project robolectric by robolectric.
the class ShadowApplicationTest method unbindServiceShouldRemoveServiceConnectionFromListOfBoundServiceConnections.
@Test
public void unbindServiceShouldRemoveServiceConnectionFromListOfBoundServiceConnections() {
final ShadowApplication shadowApplication = Shadows.shadowOf(RuntimeEnvironment.application);
final ServiceConnection expectedServiceConnection = new EmptyServiceConnection();
assertThat(shadowApplication.bindService(new Intent("connect"), expectedServiceConnection, 0)).isTrue();
assertThat(shadowApplication.getBoundServiceConnections()).hasSize(1);
assertThat(shadowApplication.getUnboundServiceConnections()).hasSize(0);
shadowApplication.unbindService(expectedServiceConnection);
assertThat(shadowApplication.getBoundServiceConnections()).hasSize(0);
assertThat(shadowApplication.getUnboundServiceConnections()).hasSize(1);
assertThat(shadowApplication.getUnboundServiceConnections().get(0)).isSameAs(expectedServiceConnection);
}
use of android.content.ServiceConnection in project robolectric by robolectric.
the class ShadowServiceTest method shouldUnbindServiceSuccessfully.
@Test
public void shouldUnbindServiceSuccessfully() {
ServiceConnection conn = Shadow.newInstanceOf(MediaScannerConnection.class);
service.unbindService(conn);
}
use of android.content.ServiceConnection in project robolectric by robolectric.
the class ShadowServiceTest method shouldUnbindServiceWithExceptionWhenRequested.
@Test(expected = IllegalArgumentException.class)
public void shouldUnbindServiceWithExceptionWhenRequested() {
ShadowApplication.getInstance().setUnbindServiceShouldThrowIllegalArgument(true);
ServiceConnection conn = Shadow.newInstanceOf(MediaScannerConnection.class);
service.unbindService(conn);
}
use of android.content.ServiceConnection in project dynamic-load-apk by singwhatiwanna.
the class MainActivity method generateContentView.
private View generateContentView(final Context context) {
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
layout.setBackgroundColor(Color.parseColor("#F79AB5"));
Button button = new Button(context);
button.setText("Start TestActivity");
layout.addView(button, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
DLIntent intent = new DLIntent(getPackageName(), TestFragmentActivity.class);
// 传递Parcelable类型的数据
intent.putExtra("person", new Person("plugin-a", 22));
intent.putExtra("dl_extra", "from DL framework");
startPluginActivityForResult(intent, 0);
}
});
Button button2 = new Button(context);
button2.setText("Start Service");
layout.addView(button2, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
DLIntent intent = new DLIntent(getPackageName(), TestService.class);
startPluginService(intent);
}
});
Button button3 = new Button(context);
button3.setText("bind Service");
layout.addView(button3, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mConnecton == null) {
mConnecton = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
}
public void onServiceConnected(ComponentName name, IBinder binder) {
int sum = ((ITestServiceInterface) binder).sum(5, 5);
Log.e("MainActivity", "onServiceConnected sum(5 + 5) = " + sum);
}
};
}
DLIntent intent = new DLIntent(getPackageName(), TestService.class);
bindPluginService(intent, mConnecton, Context.BIND_AUTO_CREATE);
}
});
Button button4 = new Button(context);
button4.setText("unbind Service");
layout.addView(button4, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
button4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (mConnecton != null) {
DLIntent intent = new DLIntent(getPackageName(), TestService.class);
unBindPluginService(intent, mConnecton);
mConnecton = null;
}
}
});
return layout;
}
Aggregations