import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public class ReaderActivity extends AppCompatActivity {
private Button scan_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader);
scan_btn = (Button) findViewById(R.id.scan_btn);
final Activity activity = this;
scan_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result.getContents() != null) {
if(result.getContents().equals("Electronics A1")){
Toast.makeText(this, "You cancelled the scanning", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this, result.getContents(),Toast.LENGTH_LONG).show();
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
Hi all, currently doing a project for my school.
now im having problems with the program.
if(result.getContents() != null) {
if(result.getContents().equals("Electronics A1")){
Toast.makeText(this, "You cancelled the scanning", Toast.LENGTH_LONG).show();
curently this if stament is being ignored by the app.
the purpose of the statement is when i scan a specific qr code it will move to a new activity or do something.
yesterday the statement was working but suddenly today it wont work......
can you guys help me?
currently new to android studio
Related
I have a simple QR code scanning app. Here's the flowchart which describes its logic.
And this is the main part:
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public class ScanActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
}
#Override
protected void onResume() {
super.onResume();
IntentIntegrator scanIntegrator = new IntentIntegrator(ScanActivity.this);
scanIntegrator.setOrientationLocked(false);
scanIntegrator.initiateScan();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanResult != null){
String strResult = scanResult.getContents();
Intent iii = new Intent(ScanActivity.this, ScanResultActivity.class);
iii.putExtra("scan_result", strResult);
startActivity(iii);
}
}
#Override
public void onBackPressed() {
moveTaskToBack(true);
}
}
If you are in ScanActivity, then the app will immediately display ScanResultActivity after scanning, which is as expected. The only issue is app won't quit when Back key is pressed. Instead, it will jump to ScanResultActivity. Of course, the scanning result is null.
How to quit the app by pressing Back key when scanning is running?
Full code: https://github.com/anta40/QRScanDemo
this method
#Override
public void onBackPressed() {
moveTaskToBack(true);
}
should call the super method, like this
#Override
public void onBackPressed() {
super.onBackPressed();
}
i want use toggle switch by voice commands like switch on and switch off so i got a code for voice recognition from a site but dont know how to trigger my toggle button thru it
The code of voice recognition i used -
package com.authorwjf.talk2me;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
protected static final int REQUEST_OK = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(i, REQUEST_OK);
} catch (Exception e) {
Toast.makeText(this, "Error initializing speech to text engine.", Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==REQUEST_OK && resultCode==RESULT_OK) {
ArrayList<String> thingsYouSaid = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
((TextView)findViewById(R.id.text1)).setText(thingsYouSaid.get(0));
}
}
}
your array thingsYouSaid have all possible string array you have . for example if I say hello it will have like [hello,aloe,hallo,no] so what you have to do is you can match your string switch off to result string array and if it is match with like "switch off" than change value of your switch from on to off likewise;
if (requestCode==REQUEST_OK && resultCode==RESULT_OK) {
ArrayList<String> thingsYouSaid = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
for(String value : thingsYouSaid.get(0)){
if(value.equalsignorecase("switch off")){
// change value for switch to off
break;
}
else if(value.equalsignorecase("switch on")){
// change value for switch to on
break;
}
}
i'm trying to integrate Zxing library, and use the barcode scanner from my app.
so, downloaded the 2 java files IntentIntegrator and IntentResult, put them into this package:
com.google.zxing.integration
where my app is in this package:
com.example.mindstormsgamepad
the code I'm using in my activity is:
package com.example.mindstormsgamepad;
import com.google.zxing.integration.IntentIntegrator;
import com.google.zxing.integration.IntentResult;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.Button;
import android.widget.TextView;
/**
* BarcodeScan Activity
*/
public class BarcodeScanActivity extends CommonActivity implements OnClickListener{
/** Debug */
protected String TAG_SUB = "BarcodeScanActivity";
private Button scanBtn;
private TextView formatTxt, contentTxt;
#Override
public void onCreate(Bundle savedInstanceState) {
initTabSub( TAG_SUB );
log_d( "onCreate" );
super.onCreate( savedInstanceState );
/* set the layout on the screen */
View view = getLayoutInflater().inflate( R.layout.activity_barcode_scan, null );
setContentView( view );
/* Initialization of Bluetooth */
initManager( view );
setTitleName( R.string.activity_barcodescan );
initButtonBack();
initInputDeviceManager();
Toast.makeText(BarcodeScanActivity.this, "QR Scan!", Toast.LENGTH_SHORT).show();
/* Initialization of Scanning */
scanBtn = (Button)findViewById(R.id.scan_button);
formatTxt = (TextView)findViewById(R.id.scan_format);
contentTxt = (TextView)findViewById(R.id.scan_content);
scanBtn.setOnClickListener(this);
}
// --- onCreate end ---
/**
* === onResume ===
*/
#Override
public void onResume() {
log_d( "onResume()" );
super.onResume();
startService();
mInputDeviceManager.register();
}
/**
* === onPause ===
*/
#Override
public void onPause() {
log_d( "onPause()" );
super.onPause();
sendStop();
mInputDeviceManager.unregister();
}
#Override
public void onClick(View v) {
// Start Scan
if(v.getId()==R.id.scan_button){
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
//retrieve scan result
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
//we have a result
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
formatTxt.setText("FORMAT: " + scanFormat);
contentTxt.setText("CONTENT: " + scanContent);
}else{
Toast toast = Toast.makeText(getApplicationContext(),
"No Barcode data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
}
but I'm getting this error,
The constructor IntentIntegrator(BarcodeScanActivity) is undefined
and
The method initiateScan(Activity) in the type IntentIntegrator is not applicable for the arguments ()
on these lines:
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
I'm new to android programming,
how to solve this problem?
thanks for your help.
Try This :
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE,PRODUCT_MODE");
startActivityForResult(intent, 0);
For Result Do the code in onActivityResult method
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Toast.makeText(getActivity(), "result ", 1000).show();
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
//do code here
}
else if (resultCode == RESULT_CANCELED) {
//do code here
}
}
This means your Activity does not ultimately extend android.app.Activity. see what CommonActivity extends. Otherwise maybe you somehow have an old or wrong copy of the IntentIntegrator.
I am integrating the zxing scanner into my android application and I am coming across a weird situation where the onActivityResult (in the activity initiating the scan) is being called before the scanning intent opens. I have looked at several examples and my code seems to match what I am seeing in many of the tutorials. Here is the code for the activity.
package com.honeydewit;
import java.util.ArrayList;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.honeydewit.adapters.ListItemAdapter;
import com.honeydewit.listeners.OneOffClickListener;
import com.honeydewit.pojos.BasicList;
import com.honeydewit.pojos.ListItem;
public class ListHomeActivity extends BasicActivity{
private ImageButton addItemBtn;
private ImageButton addByScanBtn;
private ArrayList<ListItem> lists = new ArrayList<ListItem>();
public static ListItemAdapter listAdapter;
private TextView headerTxt;
private BasicList basicList;
private ListView listView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listshome);
listView =(ListView) findViewById(R.id.list);
basicList = getApplicationContext().getCurrentList();
headerTxt = (TextView)findViewById(R.id.headerTxt);
headerTxt.setText(basicList.getName());
headerTxt.setTypeface(getApplicationContext().getTypeface());
//add button
addItemBtn = (ImageButton)findViewById(R.id.add);
addItemBtn.setOnClickListener(new OneOffClickListener() {
#Override
public void onClick(View v) {
addToList(v, basicList);
}
});
addByScanBtn = (ImageButton)findViewById(R.id.addByScan);
addByScanBtn.setVisibility(View.VISIBLE);
addByScanBtn.setOnClickListener(new OneOffClickListener() {
#Override
public void onClick(View v) {
addToListByScan(v, basicList);
}
});
setupListAdapter(basicList.get_id());
}
private void setupListAdapter(int listId) {
populateListItems(listId);
listAdapter = new ListItemAdapter(this, R.layout.listrow, lists);
listView.setAdapter(listAdapter);
}
private void populateListItems(int listId) {
ArrayList<ListItem> items = (ArrayList<ListItem>)getApplicationContext().getShoppingListDbHelper().getShoppingListItems(listId);
for(ListItem item : items ) {
lists.add(item);
}
}
private void addToList(View view, BasicList list) {
if(basicList.getListTypeId() == Constants.TODO_LIST_TYPE_CDE) {
Intent newListIntent = new Intent(getBaseContext(), ToDoItemActivity.class);
startActivityForResult(newListIntent, 1);
}
else {
Intent newListIntent = new Intent(getBaseContext(), ItemActivity.class);
startActivityForResult(newListIntent, 1);
}
}
private void addToListByScan(View view, BasicList list) {
try {
IntentIntegrator zxingIntegrator = new IntentIntegrator(this);
zxingIntegrator.initiateScan();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "ERROR:" + e, 1).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
Log.d(getClass().getName(), scanResult.getContents());
Log.d(getClass().getName(), scanResult.getFormatName());
Log.d(getClass().getName(),data.getStringExtra("SCAN_RESULT_FORMAT"));
Log.d(getClass().getName(),data.getStringExtra("SCAN_RESULT"));
}
}
}
}
onActivityResult is called by Android whenever your app needs to be told about an Intent. That could be from lots of places. You know by looking at the request code and comparing it to the one in IntentIntegrator. Or let that class do all this for you.
My guess is your intent-filter is too broad and you're hearing things you don't expect.
I have the zxing library imported into my project and the scanner works like a charm but when i scan a qr code it says Qr code found and goes back to the menu i had set up is there any way to show the result and set it to open the url
package com.Qrgolf.App;
import java.util.regex.Pattern;
import com.google.zxing.Result;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button scan = (Button) findViewById(R.id.SCANBUTTON);
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
});
};
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");
// Handle successful scan
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
}
You should consider going back to your old questions and accepting answers if they were correct.
Also you need to change the onActivityResult() method to do whatever it is that you want to do with the resulting String from the QR.
here is an example:
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");
// Handle successful scan
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(android.net.Uri.parse(contents));
startActivity(intent);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}