来自onActivityResult的Android全局变量

时间:2015-05-22 17:13:27

标签: android global onactivityresult

如何从onActivityResult创建全局变量, 我在方法中得到了结果,但结果是null out方法 我尝试了3个解决方案:使用静态变量,使用单例类和 通过扩展Application类但结果相同...... 任何帮助!

3 个答案:

答案 0 :(得分:0)

// *** AsynTask     私有类MyTask扩展了AsyncTask {

    @Override
    protected void onPreExecute() {         
        tasks.add(this);

    }

    @Override
    protected String doInBackground(RequestPackage... params) {

        String content = HttpManager.getData(params[0]);
        return content;
    }

    @Override
    protected void onPostExecute(String result) {
        tasks.remove(this); 

        displayWebview(result);

    }       
}

答案 1 :(得分:0)

Globals g = Globals.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_menu);


    wvResult = (WebView) findViewById(R.id.webResult);
    btnScan = (Button) findViewById(R.id.btnScan);


    btnScan.setOnClickListener(this);


    Log.d("maher", "id  "+g.getSomevalue());
    tasks = new ArrayList<>();
}


@Override
public void onClick(View v) {

    switch (v.getId()) {
    case R.id.btnScan:

        if (isCameraAvailable()) {
            Intent intent = new Intent(this, ZBarScannerActivity.class);
            intent.putExtra(ZBarConstants.SCAN_MODES, new int[]{Symbol.QRCODE});

            startActivityForResult(intent, ZBAR_SCANNER_REQUEST);

        } else {
            Toast.makeText(this, "Rear Facing Camera Unavailable", Toast.LENGTH_SHORT).show();
        }
        //******

        if (isOnline()) {
            requestData("myService.php");
                    } else {
            Toast.makeText(MenuActivity.this, "Network isn't available", Toast.LENGTH_LONG).show();
        }
        break;
}

public boolean isCameraAvailable() {
    PackageManager pm = getPackageManager();
    return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case ZBAR_SCANNER_REQUEST:
        case ZBAR_QR_SCANNER_REQUEST:
            if (resultCode == RESULT_OK) {
                 myResult =  data.getStringExtra(ZBarConstants.SCAN_RESULT);
                 myIdClt = myResult.substring(34);
                if (myResult.startsWith("myPrefix")) {
                    Toast.makeText(this, "Scan Result = " + myIdClt, Toast.LENGTH_LONG).show();
                    g.setSomevalue(myIdClt);
                } else {
                    Toast.makeText(this, "QR code is not valide!! ", Toast.LENGTH_LONG).show();

                }                   

            } else if(resultCode == RESULT_CANCELED && data != null) {
                String error = data.getStringExtra(ZBarConstants.ERROR_INFO);
                if(!TextUtils.isEmpty(error)) {
                    Toast.makeText(this, error, Toast.LENGTH_LONG).show();
                }
            }
            break;
    }

}

答案 2 :(得分:0)

protected void displayWebview(String result) {
    //wvResult.getSettings().setJavaScriptEnabled(true);
    wvResult.loadData(result, "text/html", "UTF-8");

}

//*** isOnLine
protected boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    } else {
        return false;
    }
}
//***execution task

private void requestData(String uri) {
    RequestPackage p = new RequestPackage();
    p.setMethod("GET");
    p.setUri(uri);
    p.setParam("idClient", myIdClt);
    MyTask task = new MyTask();
    task.execute(p);
}