gpt4 book ai didi

Java for Android 从 AsyncTask 中调用 getPackageName()

转载 作者:行者123 更新时间:2023-12-01 10:34:46 25 4
gpt4 key购买 nike

我正在使用 InAppBilling 开发 Android 应用程序。我最近按照 Google 的建议将以下代码从我的主 Activity 移至 AsyncTask:

class GetItemList extends AsyncTask<Integer, Integer, Long> {

IInAppBillingService mService;

ServiceConnection mServiceConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
}

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

@Override
protected Long doInBackground(Integer... params) {
ArrayList<String> skuList = new ArrayList<String> ();
skuList.add("i001");
skuList.add("i002");
Bundle querySkus = new Bundle();
querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
Bundle skuDetails = null;
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;
object = new JSONObject(thisResponse);
String sku = object.getString("productId");
String price = object.getString("price");
String mPremiumUpgradePrice;
String mGasPrice;
if (sku.equals("i001")) mPremiumUpgradePrice = price;
else if (sku.equals("i002")) mGasPrice = price;
}
}
} catch (RemoteException e) {
// TODO Auto-generated catch block
Log.d("Synch Billing", "Error Remote: " + e.getMessage());
e.printStackTrace();
}
catch (JSONException e) {
// TODO Auto-generated catch block
Log.d("Synch Billing", "Error JSON: " + e.getMessage());
e.printStackTrace();
}
return null;
}

}

我的问题是,对 getPackageName()(try block 的第一行)的调用给出了错误:“对于任务 GetItemList 未定义方法 getPackageName()。”如何从 AsyncTask 中调用 getPackageName()?我尝试过 GetContextWrapper.getPackageName()getApplicationContext.getPackageName()getResources.getPackageName()

<小时/>

根据下面 mixel 的答案更正了代码:

    package com.myknitcards;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.vending.billing.IInAppBillingService;

import android.app.Activity;
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class AvailableCards extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_available_cards);
String packagename = this.getPackageName();
new GetItemList(packagename).execute();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.available_cards, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

class GetItemList extends AsyncTask<Integer, Integer, Long> {

private String pName;

GetItemList(String packagename){
pName = packagename;
}

IInAppBillingService mService;

ServiceConnection mServiceConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
}

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

@Override
protected Long doInBackground(Integer... params) {
ArrayList<String> skuList = new ArrayList<String> ();
skuList.add("i001");
skuList.add("i002");
Bundle querySkus = new Bundle();
querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
Bundle skuDetails = null;
try {
skuDetails = mService.getSkuDetails(3, pName, "inapp", querySkus);
int response = skuDetails.getInt("RESPONSE_CODE");
if (response == 0) {
ArrayList<String> responseList
= skuDetails.getStringArrayList("DETAILS_LIST");
for (String thisResponse : responseList) {
JSONObject object;
object = new JSONObject(thisResponse);
String sku = object.getString("productId");
String price = object.getString("price");
String mPremiumUpgradePrice;
String mGasPrice;
if (sku.equals("i001")) mPremiumUpgradePrice = price;
else if (sku.equals("i002")) mGasPrice = price;
}
}
} catch (RemoteException e) {
// TODO Auto-generated catch block
Log.d("Synch Billing", "Error Remote: " + e.getMessage());
e.printStackTrace();
}
catch (JSONException e) {
// TODO Auto-generated catch block
Log.d("Synch Billing", "Error JSON: " + e.getMessage());
e.printStackTrace();
}
return null;
}
}

最佳答案

GetItemList 添加构造函数,该构造函数接受 packageName 并将其分配给私有(private)字段。然后在mService.getSkuDetails()中使用它。

当您在 Activity 中实例化 GetItemList 时,会将 getPackageName() 返回的值传递给 GetItemList 构造函数。

关于Java for Android 从 AsyncTask 中调用 getPackageName(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34832187/

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