gpt4 book ai didi

android - InAppBillingService 绑定(bind)失败

转载 作者:搜寻专家 更新时间:2023-11-01 08:50:05 24 4
gpt4 key购买 nike

我正在做基本的应用计费,但服务无法绑定(bind)。

这是我的主要 Activity :

public class MainActivity extends Activity {

IInAppBillingService mservice;
ServiceConnection connection;
String inappid = "android.test.purchased"; // replace this with your in-app
// product id

@Override
protected 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);
}
};
getApplicationContext()
.bindService(
new Intent(
"com.android.vending.billing.InAppBillingService.BIND"),
connection, Context.MODE_PRIVATE);

Button purchaseBtn = (Button) findViewById(R.id.purchase);
purchaseBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
ArrayList skuList = new ArrayList();
skuList.add(inappid);
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) {
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) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SendIntentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
});
}

@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) {
System.out.println("Failed to parse purchase data.");
e.printStackTrace();
}
}
}
}

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

当我运行应用程序时,bindService 方法返回 false 并且 mService 为 null。日志还给我这条消息:

Implicit intents with startService are not safe: Intent { act=com.android.vending.billing.InAppBillingService.BIND } android.content.ContextWrapper.bindService:517 com.raico.coinage.MainActivity.onCreate:51 android.app.Activity.performCreate:5231 

最佳答案

将您的 MainActivity.onCreate() 更改为调用 getApplicationContext().bindService() 与此类似:

Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
getApplicationContext().bindService(serviceIntent, connection, Context.MODE_PRIVATE);

这将使 Intent 明确,这更安全并且也应该适用于 Lollipop。

关于android - InAppBillingService 绑定(bind)失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24328337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com