我在应用计费方面做了基本工作,但服务无法绑定。
这是我的主要活动:
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
答案 0 :(得分:3)
更改您的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);
这将使意图明确,更安全,也应该在Lollipop上工作。