Android 无法解析方法 startIntentSenderForResult()
Android Cannot resolve method startIntentSenderForResult()
我尝试按照此页面中的教程进行操作 http://developer.android.com/google/play/billing/billing_integrate.html
但是当我尝试使用方法 startIntentSenderForResult() 时遇到了一个小问题,我不明白为什么,它说他无法解析该方法。
我会粘贴我的代码,因为也许你可以告诉我发生了什么,但在我给你一些解释之前:
我的 StoreActivity 中有 2 个按钮,我使用来自 google 市场的信息和方法 queryInventory() 实现,当我单击这些按钮后,我从我的文件 BillingServiceConnection 启动方法 buyThings() .
文件如下:
StoreActivity
public class StoreActivity extends Activity implements BillingQuery{
private static final Logger logger = Logger.getLogger("StoreActivity");
private BillingServiceConnection billingServiceConnection = new BillingServiceConnection(this);
private Button storeButton;
private Button storeButton2;
ArrayList<StoreItem> items = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store);
configureBilling();
}
private void configureBilling() {
Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
bindService(serviceIntent, billingServiceConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (billingServiceConnection != null) {
unbindService(billingServiceConnection);
}
}
@Override
public void onBillingQuerySuccess(ArrayList<StoreItem> storeItems) {
items = storeItems;
storeButton = (Button) findViewById(R.id.activity_menu_store_button);
storeButton.setText(storeItems.get(0).getName() + " = " + storeItems.get(0).getPrice());
storeButton2 = (Button) findViewById(R.id.activity_menu_store_button_2);
storeButton2.setText(storeItems.get(1).getName() + " = " + storeItems.get(1).getPrice());
}
public void storeButton(View view) {
billingServiceConnection.buyJocker(items.get(0).getProductId());
}
public void storeButton2(View view) {
billingServiceConnection.buyJocker(items.get(1).getProductId());
}
计费服务连接
public class BillingServiceConnection implements ServiceConnection {
private static final Logger logger = Logger.getLogger("BillingServiceConnection");
private static final String SKU_PACK_SMALL = "small_piece_pack";
private static final String SKU_PACK_MEDIUM = "medium_piece_pack";
private IInAppBillingService billingService = null;
private BillingQuery queryCallback;
public BillingServiceConnection(BillingQuery queryCallback) {
this.queryCallback = queryCallback;
}
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
billingService = IInAppBillingService.Stub.asInterface(iBinder);
queryInventory();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
billingService = null;
}
public void buyThings(String productId){
Bundle buyIntentBundle = null;
try {
buyIntentBundle = billingService.getBuyIntent(3, "fr.xxx.xxx", productId, "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
} catch (RemoteException e) {
e.printStackTrace();
}
PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
startIntentSenderForResult(pendingIntent.getIntentSender(),1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0),Integer.valueOf(0));
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1001) {
int responseCode = data.getIntExtra("RESPONSE_CODE", 0);
String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE");
if (resultCode == RESULT_OK) {
try {
JSONObject jo = new JSONObject(purchaseData);
String sku = jo.getString("productId");
alert("You have bought the " + sku + ". Excellent choice, adventurer!");
}
catch (JSONException e) {
alert("Failed to parse purchase data.");
e.printStackTrace();
}
}
}
}
public void queryInventory() {
ArrayList<StoreItem> items = new ArrayList<>();
ArrayList<String> skuList = new ArrayList<String>();
skuList.add(SKU_PACK_SMALL);
skuList.add(SKU_PACK_MEDIUM);
Bundle querySkus = new Bundle();
querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
try {
Bundle skuDetails = billingService.getSkuDetails(3, "fr.xxx.xxx", "inapp", querySkus);
int response = skuDetails.getInt("RESPONSE_CODE");
if (response != 0) {
logger.warning("Bad server response code");
return;
}
else if (response == 0) {
ArrayList<String> responseList = skuDetails.getStringArrayList("DETAILS_LIST");
logger.info("Details list size : " + responseList.size());
for (String thisResponse : responseList) {
JSONObject object = new JSONObject(thisResponse);
String sku = object.getString("productId");
String price = object.getString("price");
if (sku.equals(SKU_PACK_SMALL)){
items.add(new StoreItem("10", price, sku));
}
else if (sku.equals(SKU_PACK_MEDIUM)){
items.add(new StoreItem("60", price, sku));
}
}
}
logger.info("Server response " + skuDetails.toString());
} catch (RemoteException | JSONException ex) {
logger.warning(ex.toString());
}
queryCallback.onBillingQuerySuccess(items);
}
如果有人能帮助我,我将不胜感激。
谢谢
好的,
我自己找到了回复,问题是 startIntentSenderForResult 必须在 activity 中,所以我不得不这样做:
计费服务连接:
public PendingIntent buyJocker(String productId){
Bundle buyIntentBundle = null;
try {
buyIntentBundle = billingService.getBuyIntent(3, "fr.xxx.xxx", productId, "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
} catch (RemoteException e) {
e.printStackTrace();
}
PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
return pendingIntent;
}
商店活动:
public void storeButton(View view) {
//storeButton.setBackgroundResource(R.drawable.joker_select);
PendingIntent pendingIntent = billingServiceConnection.buyJocker(items.get(0).getProductId());
try {
startIntentSenderForResult(pendingIntent.getIntentSender(), 1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
谢谢。
我尝试按照此页面中的教程进行操作 http://developer.android.com/google/play/billing/billing_integrate.html
但是当我尝试使用方法 startIntentSenderForResult() 时遇到了一个小问题,我不明白为什么,它说他无法解析该方法。
我会粘贴我的代码,因为也许你可以告诉我发生了什么,但在我给你一些解释之前:
我的 StoreActivity 中有 2 个按钮,我使用来自 google 市场的信息和方法 queryInventory() 实现,当我单击这些按钮后,我从我的文件 BillingServiceConnection 启动方法 buyThings() .
文件如下:
StoreActivity
public class StoreActivity extends Activity implements BillingQuery{
private static final Logger logger = Logger.getLogger("StoreActivity");
private BillingServiceConnection billingServiceConnection = new BillingServiceConnection(this);
private Button storeButton;
private Button storeButton2;
ArrayList<StoreItem> items = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store);
configureBilling();
}
private void configureBilling() {
Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
bindService(serviceIntent, billingServiceConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (billingServiceConnection != null) {
unbindService(billingServiceConnection);
}
}
@Override
public void onBillingQuerySuccess(ArrayList<StoreItem> storeItems) {
items = storeItems;
storeButton = (Button) findViewById(R.id.activity_menu_store_button);
storeButton.setText(storeItems.get(0).getName() + " = " + storeItems.get(0).getPrice());
storeButton2 = (Button) findViewById(R.id.activity_menu_store_button_2);
storeButton2.setText(storeItems.get(1).getName() + " = " + storeItems.get(1).getPrice());
}
public void storeButton(View view) {
billingServiceConnection.buyJocker(items.get(0).getProductId());
}
public void storeButton2(View view) {
billingServiceConnection.buyJocker(items.get(1).getProductId());
}
计费服务连接
public class BillingServiceConnection implements ServiceConnection {
private static final Logger logger = Logger.getLogger("BillingServiceConnection");
private static final String SKU_PACK_SMALL = "small_piece_pack";
private static final String SKU_PACK_MEDIUM = "medium_piece_pack";
private IInAppBillingService billingService = null;
private BillingQuery queryCallback;
public BillingServiceConnection(BillingQuery queryCallback) {
this.queryCallback = queryCallback;
}
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
billingService = IInAppBillingService.Stub.asInterface(iBinder);
queryInventory();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
billingService = null;
}
public void buyThings(String productId){
Bundle buyIntentBundle = null;
try {
buyIntentBundle = billingService.getBuyIntent(3, "fr.xxx.xxx", productId, "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
} catch (RemoteException e) {
e.printStackTrace();
}
PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
startIntentSenderForResult(pendingIntent.getIntentSender(),1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0),Integer.valueOf(0));
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1001) {
int responseCode = data.getIntExtra("RESPONSE_CODE", 0);
String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE");
if (resultCode == RESULT_OK) {
try {
JSONObject jo = new JSONObject(purchaseData);
String sku = jo.getString("productId");
alert("You have bought the " + sku + ". Excellent choice, adventurer!");
}
catch (JSONException e) {
alert("Failed to parse purchase data.");
e.printStackTrace();
}
}
}
}
public void queryInventory() {
ArrayList<StoreItem> items = new ArrayList<>();
ArrayList<String> skuList = new ArrayList<String>();
skuList.add(SKU_PACK_SMALL);
skuList.add(SKU_PACK_MEDIUM);
Bundle querySkus = new Bundle();
querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
try {
Bundle skuDetails = billingService.getSkuDetails(3, "fr.xxx.xxx", "inapp", querySkus);
int response = skuDetails.getInt("RESPONSE_CODE");
if (response != 0) {
logger.warning("Bad server response code");
return;
}
else if (response == 0) {
ArrayList<String> responseList = skuDetails.getStringArrayList("DETAILS_LIST");
logger.info("Details list size : " + responseList.size());
for (String thisResponse : responseList) {
JSONObject object = new JSONObject(thisResponse);
String sku = object.getString("productId");
String price = object.getString("price");
if (sku.equals(SKU_PACK_SMALL)){
items.add(new StoreItem("10", price, sku));
}
else if (sku.equals(SKU_PACK_MEDIUM)){
items.add(new StoreItem("60", price, sku));
}
}
}
logger.info("Server response " + skuDetails.toString());
} catch (RemoteException | JSONException ex) {
logger.warning(ex.toString());
}
queryCallback.onBillingQuerySuccess(items);
}
如果有人能帮助我,我将不胜感激。 谢谢
好的,
我自己找到了回复,问题是 startIntentSenderForResult 必须在 activity 中,所以我不得不这样做:
计费服务连接:
public PendingIntent buyJocker(String productId){
Bundle buyIntentBundle = null;
try {
buyIntentBundle = billingService.getBuyIntent(3, "fr.xxx.xxx", productId, "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
} catch (RemoteException e) {
e.printStackTrace();
}
PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
return pendingIntent;
}
商店活动:
public void storeButton(View view) {
//storeButton.setBackgroundResource(R.drawable.joker_select);
PendingIntent pendingIntent = billingServiceConnection.buyJocker(items.get(0).getProductId());
try {
startIntentSenderForResult(pendingIntent.getIntentSender(), 1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
谢谢。