2016-01-16 13 views
0

我正在使用InAppBilling處理Android應用程序。我最近搬到離我的主要活動如下代碼到的AsyncTask的建議,谷歌:適用於Android的Java在AsyncTask中調用getPackageName()

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

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

而且當您在您的活動中實例化GetItemList時,傳遞值由getPackageName()返回到GetItemList的構造函數。

+0

謝謝mixel。我會嘗試這個並報告回來。 – Melanie

+0

再一次,謝謝mixel。這工作很好。我在我的問題中添加了更正的代碼以供他人蔘考。 – Melanie