Android for Java从AsyncTask

时间:2016-01-16 21:12:12

标签: java android android-asynctask in-app-billing

我正在使用InAppBilling开发Android应用。我最近将以下代码从我的主Activity转移到AsyncTask,如Google推荐的那样:

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块的第一行)的调用给出了错误,“方法getPackageName()未定义任务GetItemList。”如何在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;
    }
}

1 个答案:

答案 0 :(得分:1)

将构造函数添加到接受GetItemList的{​​{1}}并将其分配给私有字段。然后在packageName中使用它。

当您在mService.getSkuDetails()返回给GetItemList构造函数的活动传递值中实例化getPackageName()时。