Search in sources :

Example 11 with ServiceConnection

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."));
        }
    }
}
Also used : ServiceConnection(android.content.ServiceConnection) IBinder(android.os.IBinder) ComponentName(android.content.ComponentName) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) RemoteException(android.os.RemoteException)

Example 12 with ServiceConnection

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);
}
Also used : ServiceConnection(android.content.ServiceConnection) Intent(android.content.Intent) Test(org.junit.Test)

Example 13 with ServiceConnection

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);
}
Also used : ServiceConnection(android.content.ServiceConnection) Test(org.junit.Test)

Example 14 with ServiceConnection

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);
}
Also used : ServiceConnection(android.content.ServiceConnection) Test(org.junit.Test)

Example 15 with ServiceConnection

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;
}
Also used : ServiceConnection(android.content.ServiceConnection) LayoutParams(android.view.ViewGroup.LayoutParams) View(android.view.View) IBinder(android.os.IBinder) Button(android.widget.Button) OnClickListener(android.view.View.OnClickListener) ComponentName(android.content.ComponentName) ITestServiceInterface(com.ryg.dynamicload.service.ITestServiceInterface) DLIntent(com.ryg.dynamicload.internal.DLIntent) LinearLayout(android.widget.LinearLayout)

Aggregations

ServiceConnection (android.content.ServiceConnection)122 ComponentName (android.content.ComponentName)95 Intent (android.content.Intent)95 IBinder (android.os.IBinder)94 RemoteException (android.os.RemoteException)75 PendingIntent (android.app.PendingIntent)46 UserHandle (android.os.UserHandle)23 Handler (android.os.Handler)20 Message (android.os.Message)19 Messenger (android.os.Messenger)19 IRemoteViewsFactory (com.android.internal.widget.IRemoteViewsFactory)12 IInterface (android.os.IInterface)9 ResolveInfo (android.content.pm.ResolveInfo)8 IntentFilter (android.content.IntentFilter)7 Point (android.graphics.Point)7 AndroidRuntimeException (android.util.AndroidRuntimeException)7 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)7 Test (org.junit.Test)7 FilterComparison (android.content.Intent.FilterComparison)6 ReceiverCallNotAllowedException (android.content.ReceiverCallNotAllowedException)6