如何访问库的资源

时间:2014-12-05 12:45:07

标签: android jar android-asynctask android-resources android-library

我正在尝试创建一个提供AsyncTask操作的Android库。我创建了我的AsyncTask,如下所示。但是,我不知道如何访问图书馆的资源。上下文字段由使用该库的活动设置。我曾经通过调用getActivity()。getResources()来访问资源。但是这个AsyncTask的上下文来自使用它的项目,我不知道如何访问库的资源。我怎样才能做到这一点?

public class CheckBalanceAsyncTask extends AsyncTask<Void, Void, Void> {
private Context mContext;
String json;
JSONObject jsonObject,jsonObjResult;
JSONArray jsonArray;
String message,balance;
ProgressDialog progress;
    public CheckBalanceAsyncTask (Context context){
         mContext = context;
         //progress = new ProgressDialog(mContext);
    }


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
//          progress.setCancelable(false);
//          progress.setMessage("please wait");//getString(R.string.pleasewait));
//          progress.setTitle("waiting");//getString(R.string.loading));
//          progress.setIcon(R.drawable.ic);
//          progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//          progress.setProgress(0);
//          progress.setMax(100);
//          progress.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            String aliasNo = "";

            PackageInfo pinfo = null;
            try {
                pinfo = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
            } catch (NameNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            String versionName = pinfo.versionName;

            String cardServiceUrl = "blabla";
            Log.e("LOGGGGG", cardServiceUrl);

            try {
                json = JSONParser.getJSONFromUrl(cardServiceUrl);
                try {
                    jsonObject = new JSONObject(json);
                } catch (JSONException e) {
                    Log.e("JSON Parser",
                            "Error creating json object" + e.toString());
                }
                jsonArray = jsonObject.getJSONArray("cardlist");

                jsonObjResult = jsonObject.getJSONObject("result");

                message = jsonObjResult.get("message").toString();

                Log.e("MESSAGE", "" + message);

                JSONObject row = jsonArray.getJSONObject(0);

                balance = row.optString("balance");

            }

            catch (JSONException e) {

                Log.e("json", "doInBackground2");
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            //progress.dismiss();

            try {
                if (jsonObjResult.get("message").toString()
                        .equalsIgnoreCase("ok")) {
                    Toast.makeText(mContext, balance+" TL", Toast.LENGTH_SHORT).show();
                }

                else {
                    Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }

1 个答案:

答案 0 :(得分:1)

Ressources会自动与主应用程序合并,无需执行特定的操作来访问字符串,可绘制,布局等... getActivity()。getResources()可以正常工作。