2013-06-20 105 views
3

試圖將應用程序購買中的一些添加到我正在處理的應用程序中,但情況並不會那麼順利。Android InAppBilling onServiceConnected從未呼籲

我有一個FragmentActivity這樣的:

public class TestInAppBilling extends FragmentActivity{ 

//Application context reference 
private static Context context; 

/* 
Billing stuff 
*/ 
private IInAppBillingService mService; 
private ServiceConnection mServiceConn; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.test_layout); 

    context = getApplicationContext(); 

    if(mServiceConn == null){ 
     mServiceConn = new ServiceConnection() { 
      @Override 
      public void onServiceDisconnected(ComponentName name) { 
       mService = null; 
      } 

      @Override 
      public void onServiceConnected(ComponentName name, 
              IBinder service) { 
       mService = IInAppBillingService.Stub.asInterface(service); 
       System.out.println("Bound!"); 
      } 
     }; 

     context.bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), mServiceConn, Context.BIND_AUTO_CREATE); 
    } 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    if (mServiceConn != null) { 
     unbindService(mServiceConn); 
    } 
} 
} 

但由於某些原因,onServiceConnected回調從未發生過。

任何人都知道是什麼造成它?

回答

0

我認爲你解決了它。無論如何,我有同樣的問題,我只是修復它。 要使它發揮作用刪除這一行:

context.bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), mServiceConn, Context.BIND_AUTO_CREATE); 

並添加此:

setContentView(R.layout.test_layout); 
context = getApplicationContext(); 
Intent intent = new Intent("com.android.vending.billing.InAppBillingService.BIND"); 
intent.setPackage("com.android.vending"); 
getContext().bindService(intent, mServiceConn, getActivity().BIND_AUTO_CREATE);