如何在app预订android中实现app id

时间:2014-08-11 08:47:06

标签: android google-play google-play-services

我已经在应用订阅中关注了一些示例,但我不确定在google play store中生成app_id的位置..请帮助我这个小东西..

这是我的班级,这项工作是为了测试,但不知道如何在实际案例中实施

public class MainActivity extends ActionBarActivity implements OnClickListener {

private WebView webView;
private Button subscription;
IInAppBillingService mservice;
ServiceConnection connection;
String inappid = "android.test.purchased";

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    connection = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mservice = null;

        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mservice = IInAppBillingService.Stub.asInterface(service);
        }
    };

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



public void showDialog(View v) {
    ArrayList skuList = new ArrayList();
            skuList.add(inappid);
            Log.i("TAG", "CLICK");
            Bundle querySkus = new Bundle();
            querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
            Bundle skuDetails;
            try {
                skuDetails = mservice.getSkuDetails(3, getPackageName(),
                        "inapp", querySkus);

                int response = skuDetails.getInt("RESPONSE_CODE");
                if (response == 0) {

                    ArrayList<String> responseList = skuDetails
                            .getStringArrayList("DETAILS_LIST");

                    for (String thisResponse : responseList) {
                        Log.i("PRICE_TAG", thisResponse);
                        JSONObject object = new JSONObject(thisResponse);
                        String sku = object.getString("productId");
                        String price = object.getString("price");
                        if (sku.equals(inappid)) {
                            System.out.println("price " + price);
                            Bundle buyIntentBundle = mservice
                                    .getBuyIntent(3, getPackageName(), sku,
                                            "inapp",
                                            "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
                            PendingIntent pendingIntent = buyIntentBundle
                                    .getParcelable("BUY_INTENT");
                            startIntentSenderForResult(
                                    pendingIntent.getIntentSender(), 1001,
                                    new Intent(), Integer.valueOf(0),
                                    Integer.valueOf(0), Integer.valueOf(0));
                        }
                    }
                }
            } catch (RemoteException e) {

                Toast.makeText(getBaseContext(), "Remote erroer",
                        Toast.LENGTH_SHORT).show();
            } catch (JSONException e) {

                Toast.makeText(getBaseContext(), "json error",
                        Toast.LENGTH_SHORT).show();
            } catch (SendIntentException e) {

                Toast.makeText(getBaseContext(), "sending intent error",
                        Toast.LENGTH_SHORT).show();
            }

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1001) {
        String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");

        if (resultCode == RESULT_OK) {
            try {
                JSONObject jo = new JSONObject(purchaseData);
                String sku = jo.getString(inappid);
                Toast.makeText(
                        MainActivity.this,
                        "You have bought the " + sku
                                + ". Excellent choice,adventurer!",
                        Toast.LENGTH_LONG).show();

            } catch (JSONException e) {
                Toast.makeText(this.getBaseContext(),
                        "Failed to parse data", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

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

@Override
public void onClick(View arg0) {
    showDialog(arg0);

}

0 个答案:

没有答案