簡體   English   中英

適用於Android的Java從AsyncTask中調用getPackageName()

[英]Java for Android Call getPackageName() from within an AsyncTask

我正在使用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塊的第一行)給出了錯誤,“未為任務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;
    }
}

將構造函數添加到接受packageName GetItemList ,並將其分配給私有字段。 然后在mService.getSkuDetails()使用它。

當您在活動中實例化GetItemList ,將getPackageName()返回的值傳遞給GetItemList構造函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM