Android全局变量从onActivityResult



如何创建全局变量从onActivityResult,我有结果的方法,但结果是空的方法我尝试了3个解决方案:与静态变量,通过使用单例类和通过扩展Application类,但结果相同…任何帮助!

//*** 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);
    }       
}
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;
    }
}
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);
}

相关内容

  • 没有找到相关文章

最新更新