我有一个应用程序,在登录屏幕上,我有QR扫描仪按钮。当用户点击按钮时,QR扫描仪应用程序打开,扫描完成后,它会向用户提供消息。
目前,点击按钮时,QR扫描仪打开,能够从QR码中捕获数据,但它会再次重新打开。以下是执行模式:
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
有人可以帮忙:这是我的档案:
的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>
这是我的布局文件:
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>
这是我放置QR码扫描仪按钮的代码:
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);
}
}
这是QRScanner代码:
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();
}
}
}
}
请帮助解决多个不必要的QR扫描仪打开问题。
提前致谢!
答案 0 :(得分:0)
在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.
}
根据某些条件,您必须启动意图。