2017-07-26 48 views
1

我在我的ZXing二維碼掃描儀上遇到問題。問題是當我從網頁或新活動按下後退按鈕時,掃描儀將停止工作,無法掃描任何東西。誰能告訴我我的錯誤在哪裏? :x 提前致謝。Android Zxing相機不能在後臺工作

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 
import android.util.Patterns; 
import android.content.Intent; 
import com.google.zxing.Result; 
import android.net.Uri; 

import me.dm7.barcodescanner.zxing.ZXingScannerView; 

public class ScannerActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler { 
    private ZXingScannerView zXingScannerView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_scanner); 
    } 

    public void scan(View view) { 
     zXingScannerView = new ZXingScannerView(getApplicationContext()); 
     setContentView(zXingScannerView); 
     zXingScannerView.setResultHandler(this); 
     zXingScannerView.startCamera(); 
    } 

    public void onPause() { 
     super.onPause(); 
     zXingScannerView.stopCamera(); 
    } 

    @Override 
    public void handleResult(Result result) { 
     Toast.makeText(getApplicationContext(), result.getText(), Toast.LENGTH_SHORT).show(); 
     zXingScannerView.resumeCameraPreview(this); 

     if(Patterns.WEB_URL.matcher(result.getText()).matches()) { 
      // Open URL 
      Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(result.getText())); 
      startActivity(browserIntent); 
     } 
     if(result.getText().equals("Welcome to Booth A")) { 
      // Open nextActivity 
      Intent Quiz1 = new Intent(this, QuizActivity.class); 
      startActivity(Quiz1); 
     } 

    } 

回答

0

你需要重寫onBackPressed方法ScannerActivity。你可以像

@Override 
public void onBackPressed() { 
    //do something 
} 

默認的方法調用會破壞當前的片段或活動。這是通過,其中你通過做zXingScannerView.stopCamera();停止相機。

+0

謝謝,我解決了問題 –

+0

不錯。然後您可以將其標記爲已接受。 – rushi