简体   繁体   English

Android程序多次打开QR扫描仪

[英]Android Program opens QR scanner multiple times

I am having an application where on Login screen, i have QR scanner button. 我有一个在登录屏幕上有QR扫描仪按钮的应用程序。 When user clicks the button, the QR scanner app opens and after scanning, it toasts the message to user. 当用户单击该按钮时,QR扫描仪应用程序将打开,扫描后,它将向用户发送消息。

At present, on button click, the QR scanner opens and is able to capture the data from QR code, but it reopens again. 目前,单击按钮后,QR扫描仪将打开并能够从QR码捕获数据,但会再次重新打开。 Here is the pattern of execution: 这是执行模式:

1. QR scanner opens. reads the QR code (as expected)
<after this point everything is wrong>
2. QR scanner opens again.
3. My main_screen comes back
4. QR scanner opens again and i read QR code.
5. My main screen comes back

Can someone please help: Here are my files: 有人可以帮忙吗?这是我的文件:

AndroidManifest.xml: AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.amandeepsingh.loyaltyapp2" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".SplashScreenActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
<activity android:name=".MainActivity" >
</activity>
    <activity
        android:name=".Login"
        android:label="@string/title_activity_login" >
    </activity>
    <activity
        android:name=".Register"
        android:label="@string/title_activity_register" >
    </activity>
    <!--added for QR codes scanner-->
    <activity android:name=".AndroidBarcodeQrExample"/>
    <!-- QR code ends here-->
</application>

</manifest>

This is my layout file: 这是我的布局文件:

activity_main.xml activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:background="@drawable/androidrain"
tools:context="com.example.amandeepsingh.loyaltyapp2.Login">

<TextView
    android:layout_width="wrap_content"
    android:text="Email"
    android:textColor="#FFFFFF"
    android:layout_height="wrap_content" />

<EditText
    android:id="@+id/etEmail"
    android:layout_width="match_parent"
    android:layout_marginBottom="10dp"
    android:textColor="#FFFFFF"
    android:layout_height="wrap_content" />

<TextView
    android:layout_width="wrap_content"
    android:text="Phone Number"
    android:textColor="#FFFFFF"
    android:layout_height="wrap_content" />

<EditText
    android:id="@+id/etPhoneNumber"
    android:layout_width="match_parent"
    android:layout_marginBottom="10dp"
    android:textColor="#FFFFFF"
    android:layout_height="wrap_content" />


<Button
    android:id="@+id/bLogOut"
    android:text="LogOut"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/bEarnPoints"
    android:layout_width="250dp"
    android:layout_height="80dp"
    android:layout_gravity="center"
    android:layout_margin="10dp"
    android:gravity="center"

    android:text="Earn Points"
    android:textSize="18dp" >
</Button>

<Button
    android:id="@+id/bRedeamPoints"
    android:layout_width="250dp"
    android:layout_height="80dp"
    android:layout_gravity="center"
    android:layout_margin="10dp"
    android:gravity="center"

    android:text="Redeam Points"
    android:textSize="18dp" >
</Button>

</LinearLayout>

This is the code from where i have put QR code scanner button: 这是我放置QR码扫描仪按钮的代码:

MainActivity.java MainActivity.java

public class MainActivity extends Activity implements View.OnClickListener{

Button bLogOut;
EditText etEmail,etPhoneNumber;
//this is for having access to userlocalstore to save/remove local data to user phone during log inor Log out. So that if logout is pressed
//the data is wiped off the local storage on user mobile
UserLocalStore userLocalStore;

//added for QR codes scanner
Button bEarnPoints,bRedeamPoints;
//QR code scanner ends

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

    etEmail= (EditText)findViewById(R.id.etEmail);
    etPhoneNumber =(EditText)findViewById(R.id.etPhoneNumber);
    bLogOut = (Button)findViewById(R.id.bLogOut);

    bLogOut.setOnClickListener(this);

    userLocalStore = new UserLocalStore(this);
    //added for QR codes scanner
    bEarnPoints=(Button)findViewById(R.id.bEarnPoints);
    bRedeamPoints=(Button)findViewById(R.id.bRedeamPoints);
    bEarnPoints.setOnClickListener(this);
    bRedeamPoints.setOnClickListener(this);
    //QR code ends here
}

@Override
public void onClick(View v) {
    switch(v.getId()){
        case R.id.bLogOut:
            userLocalStore.clearUserData();
            userLocalStore.setUserLoggedIn(false);

            startActivity(new Intent(this,Login.class));

            break;

        //added for QR codes scanner
        case R.id.bEarnPoints:
            startActivity(new Intent(this,AndroidBarcodeQrExample.class));
            break;

        case R.id.bRedeamPoints:
            startActivity(new Intent(this,AndroidBarcodeQrExample.class));
            break;

        //QR ends here
    }

}

@Override
protected void onStart() {
    super.onStart();

    if (authenticate() == true) {
        displayUserDetails();
    }
}

public boolean authenticate(){
    return userLocalStore.getUserLoggedIn();
}

public void displayUserDetails(){
    User user = userLocalStore.getLoggedInUser();
    etEmail.setText(user.Email);
    etPhoneNumber.setText(user.PhoneNumber+"");

}

//code below this is not being used
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, 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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

and this is the QRScanner code: 这是QRScanner代码:

AndroidBarcodeQrExample.java AndroidBarcodeQrExample.java

public class AndroidBarcodeQrExample extends Activity {
/** Called when the activity is first created. */

static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    scanQR();
}

public void scanBar(View v) {
    try {
        Intent intent = new Intent(ACTION_SCAN);
        intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
        startActivityForResult(intent, 0);
    } catch (ActivityNotFoundException anfe) {
        showDialog(AndroidBarcodeQrExample.this, "No Scanner Found", "Download a scanner code activity?", "Yes", "No").show();
    }
}

public void scanQR() {
    try {
        Intent intent = new Intent(ACTION_SCAN);
        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        startActivityForResult(intent, 0);
    } catch (ActivityNotFoundException anfe) {
        showDialog(AndroidBarcodeQrExample.this, "No Scanner Found", "Download a scanner code activity?", "Yes", "No").show();
    }
}

private static AlertDialog showDialog(final Activity act, CharSequence title, CharSequence message, CharSequence buttonYes, CharSequence buttonNo) {
    AlertDialog.Builder downloadDialog = new AlertDialog.Builder(act);
    downloadDialog.setTitle(title);
    downloadDialog.setMessage(message);
    downloadDialog.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {
            Uri uri = Uri.parse("market://search?q=pname:" + "com.google.zxing.client.android");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            try {
                act.startActivity(intent);
            } catch (ActivityNotFoundException anfe) {

            }
        }
    });
    downloadDialog.setNegativeButton(buttonNo, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {
        }
    });
    return downloadDialog.show();
}

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            String contents = intent.getStringExtra("SCAN_RESULT");
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

            Toast toast = Toast.makeText(this, "Content:" + contents + " Format:" + format, Toast.LENGTH_LONG);
            toast.show();
        }
    }
}
}

Please help with the multiple unnecessary QR scanner opening issue. 请帮助解决多个不必要的QR扫描仪打开问题。

Thanks in advance! 提前致谢!

In AndroidBarcodeQrExample Oncreate you are launching an intent without any condition. 在AndroidBarcodeQrExample Oncreate中,您正在启动没有任何条件的意图。

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scanQR();  // here everytime it starts an activity again and again.
    }

Based on some condition you have to launch an intent. 根据某些条件,您必须启动一个意图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM