我试图按照谷歌的文档使用Android应用程序内结算。但是我试图用bindService方法使用InAppBillingService对象(MSERVICE)。它返回true,但MSERVICE仍然是空。这里是我的代码
公共类PaymentActivity扩展AppCompatActivity {
IInAppBillingService mService;
ServiceConnection mServiceConn;
ArrayList<String> skuList;
Bundle querySkus;
Bundle skuDetails;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment);
Log.d("payment", "isBillingAvailable? " + isBillingAvailable(this));
String chargeString = getIntent().getStringExtra("charge");
Log.d("intentTest", "charge is: " + chargeString);
mServiceConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d("Payment", "service disconnected!");
mService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mService = IInAppBillingService.Stub.asInterface(service);
Log.d("Payment", "service connected!");
}
};
}
@Override
protected void onStart() {
super.onStart();
// Bind to IInAppBillingService
Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
this.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
try{
while(mService == null){
Thread.sleep(1000);
Log.d("payment", "sleep 1 second");
}
}catch (InterruptedException e)
{
e.printStackTrace();
}
skuList = new ArrayList<String> ();
skuList.add("premiumUpgrade");
skuList.add("gas");
querySkus = new Bundle();
querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
}
@Override
public void onDestroy() {
super.onDestroy();
if (mService != null) {
unbindService(mServiceConn);
}
}
public static boolean isBillingAvailable(Context context) {
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
List<ResolveInfo> list = packageManager.queryIntentServices(intent, 0);
return list.size() > 0;
}
}
我知道这是不好的VERT代码,因为bindService被调用的onCreate。但我想它在ASYN并没有改变。我想等到连接是通过睡眠mainTread完成。这使得试试我的应用程序在infinited循环。
我的错误信息
了java.lang.RuntimeException:无法启动活动ComponentInfo {kr.co.bigsapp.www / kr.co.bigsapp.www.activities.PaymentActivity}:显示java.lang.NullPointerException:尝试调用接口方法“android.os.Bundle com.android.vending.billing.IInAppBillingService.getSkuDetails(INT,java.lang.String中,java.lang.String中,android.os.Bundle)”上的空对象引用
请帮我TT
你的方法(循环,直到mService
为非null)是有缺陷的。事实上,这是什么原因导致该服务正在创建。
“进程”创建一个本地的,Service
不同步与bindService()
/ startService()
通话发生。你必须控制返回框架;在这种情况下,这意味着从onStart()
返回。这是因为Service
的状态由在主线程上运行的消息队列(Looper
)前进,当你被困在onStart()
,即Looper
不是“循环”。
如果您返回时,你会发现,Service.onCreate()
和ServiceConnection.onServiceConnected()
被称为在几毫秒内。