3

我正在尝试对我的应用程序实施应用内计费。

我正在阅读 developer.android.com 上的指南,查看他们的示例应用程序。

我开始编写检查是否支持应用内计费的基本方法,但是当我尝试调用 sendBillingRequest 时,我收到以下错误:

05-17 14:23:30.479: W/System.err(15332): java.lang.NullPointerException
05-17 14:23:30.489: W/System.err(15332):    at resistorcalc.main.billing.BillingService.checkBilling(BillingService.java:63)
05-17 14:23:30.489: W/System.err(15332):    at resistorcalc.main.SupportusActivity.onCreate(SupportusActivity.java:24)
05-17 14:23:30.499: W/System.err(15332):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-17 14:23:30.509: W/System.err(15332):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
05-17 14:23:30.509: W/System.err(15332):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
05-17 14:23:30.519: W/System.err(15332):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-17 14:23:30.529: W/System.err(15332):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-17 14:23:30.539: W/System.err(15332):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 14:23:30.539: W/System.err(15332):    at android.os.Looper.loop(Looper.java:130)
05-17 14:23:30.549: W/System.err(15332):    at android.app.ActivityThread.main(ActivityThread.java:3687)
05-17 14:23:30.549: W/System.err(15332):    at java.lang.reflect.Method.invokeNative(Native Method)
05-17 14:23:30.549: W/System.err(15332):    at java.lang.reflect.Method.invoke(Method.java:507)
05-17 14:23:30.549: W/System.err(15332):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-17 14:23:30.559: W/System.err(15332):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-17 14:23:30.559: W/System.err(15332):    at dalvik.system.NativeStart.main(Native Method)

billingService 类是:

package resistorcalc.main.billing;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.android.vending.billing.IMarketBillingService;

public class BillingService extends Service implements ServiceConnection {


private static final String TAG = "BillingService";
private IMarketBillingService mService;

public BillingService(){
    super();
}

@Override
public void onCreate() {        
    super.onCreate();
    try {
        Log.i(TAG, "Service starting with onCreate");            
        boolean bindResult = bindService(new Intent("com.android.vending.billing.MarketBillingService.BIND"), this, Context.BIND_AUTO_CREATE);
        if(bindResult){
            Log.i(TAG,"Market Billing Service Successfully Bound");
        } else {
            Log.e(TAG,"Market Billing Service could not be bound.");            
        }
    } catch (SecurityException e){
                Log.e(TAG,"Market Billing Service could not be bound. SecurityException: "+e);                 
    }    
}

public void setContext(Context context) {
    attachBaseContext(context);
}

/**
  * The Android system calls this when we are connected to the MarketBillingService.
  */
public void onServiceConnected(ComponentName name, IBinder service) {
    Log.i(TAG, "MarketBillingService connected.");
    mService = IMarketBillingService.Stub.asInterface(service);
}

protected Bundle makeRequestBundle(String method) {
    Bundle request = new Bundle();
    request.putString(BillingConst.BILLING_REQUEST_METHOD, method);
    request.putInt(BillingConst.BILLING_REQUEST_API_VERSION, 1);
    request.putString(BillingConst.BILLING_PACKAGE_NAME, getPackageName());
    return request;
}

public boolean checkBilling(){      
    try {
        Bundle request = makeRequestBundle("CHECK_BILLING_SUPPORTED");
        Bundle response = mService.sendBillingRequest(request);
        int code = response.getInt("RESPONSE_CODE");
        if(code==0){
            return true;
        } else {
            return false;
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.err.print(e.getMessage());
        return false;
    }
}

public void onServiceDisconnected(ComponentName arg0) {
    // TODO Auto-generated method stub

}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

}

该行发生异常:

捆绑响应 = mService.sendBillingRequest(request);

发生这种情况是因为 mService 变量为空。mService 变量在该方法上初始化:

public void onServiceConnected(ComponentName name, IBinder service) {
    Log.i(TAG, "MarketBillingService connected.");
    mService = IMarketBillingService.Stub.asInterface(service);
}

(如该指南中所述:http: //developer.android.com/guide/market/billing/billing_integrate.html#billing-service)问题是永远不会调用 onServiceConnected 。

启动服务的活动:

    public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.supportus);
    mBillingService = new BillingService();
    mBillingService.setContext(this);
    Intent intent = new Intent();
            intent.setClass(this, BillingService.class);        
    startService(intent);
    if(mBillingService.checkBilling()){
        Toast.makeText(this, "TRUE", Toast.LENGTH_SHORT);
    } else {
        Toast.makeText(this, "FALSE", Toast.LENGTH_SHORT);
    }
}

我无法理解问题所在。为什么不调用 onCreate 和 onServiceConnected 方法?查看服务生命周期,应使用 startService 调用 BillingService onCreate 方法。我哪里错了

4

2 回答 2

1

在您尝试使用 mService 之前,不会调用 onServiceConnected()。那是因为您的活动的 onServiceConnected() 在它的 onCreate() 返回之前不能被调用,它还没有完成。

基本上,您需要在服务连接后稍后再调用 checkBilling()。您可以响应服务连接来执行此操作,或者您可以稍后在用户实际尝试执行某项操作时执行此操作。

于 2012-05-17T14:48:45.107 回答
1

最后我找到了解决这个问题的方法。这是我的错。

错误在清单中,实际上我在服务中声明为

    <service android:name=".BillingService" />

但是 BillingService 类在一个子包中(resistorcalc.main.billing)。然后我将这一行替换为:

    <service android:name=".billing.BillingService" />

服务开始工作。

于 2012-05-24T08:29:24.387 回答