android


Not receiving an Intent back from Zxing barcode scanner


I know some others have had this problem but I've followed the solutions and it still isn't working for me.
I've created a new application, it has 1 activity which has 1 button (scan button) and 2 textviews (which are just going to output the formatname and contents that Zxing returns at the moment).
I have followed the ScanningViaIntent tutorial but it doesn't seem to be hitting onActivityResult
Below is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final IntentIntegrator integrator = new IntentIntegrator(this);
Button btnScan = (Button) findViewById(R.id.button1);
btnScan.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
integrator.initiateScan();
}
});
}
public void OnActivityResult(int requestCode, int resultCode, Intent intent)
{
Log.i("result", "hit line");
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
TextView tv1 = (TextView) findViewById(R.id.textView1);
TextView tv2 = (TextView) findViewById(R.id.textView2);
if(scanResult != null)
{
System.out.println("format: " + scanResult.getFormatName());
System.out.println("contents: " + scanResult.getContents());
tv1.setText(scanResult.getFormatName());
tv2.setText(scanResult.getContents());
}
else
{
tv1.setText("ERROR");
}
}
TextView1 never says "Error" so it doesn't seem that scanResult is null and my Log.i() line is never hit so I'm thinking that onActivityResult isn't even being hit.
Could it be to do with making IntentIntegrator final for the OnClick() method? When I created IntentIntegrator inside OnClick(), I used getParent() to pass the Activity to the constructor but this force closed my app with a NullReferenceException inside IntentItegrator.
Am I using the library correctly?
Thanks for your time,
Poncho
You are not actually overriding the method onActivityResult() because you have implemented OnActivityResult(). Your method is not being called as a result. Everything else looks about right.
This is the kind of thing you catch if you use #Override annotations -- good habit, since it would have caught this.
Where are you calling startActivityForResult(..)? You may want to use something like this :
Intent intentScan = new Intent(BS_PACKAGE + ".SCAN");
intentScan.addCategory(Intent.CATEGORY_DEFAULT);
// check which types of codes to scan for
if (desiredBarcodeFormats != null) {
// set the desired barcode types
StringBuilder joinedByComma = new StringBuilder();
for (String format : desiredBarcodeFormats) {
if (joinedByComma.length() > 0) {
joinedByComma.append(',');
}
joinedByComma.append(format);
}
intentScan.putExtra("SCAN_FORMATS", joinedByComma.toString());
}
String targetAppPackage = findTargetAppPackage(intentScan);
if (targetAppPackage == null) {
return showDownloadDialog();
}
intentScan.setPackage(targetAppPackage);
intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
attachMoreExtras(intentScan);
startActivityForResult(intentScan, REQUEST_CODE);
findTargetAppPackage :
private String findTargetAppPackage(Intent intent) {
PackageManager pm = activity.getPackageManager();
List<ResolveInfo> availableApps = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (availableApps != null) {
for (ResolveInfo availableApp : availableApps) {
String packageName = availableApp.activityInfo.packageName;
if (targetApplications.contains(packageName)) {
return packageName;
}
}
}
return null;
}
To see a more complete example go here.
You need to get the latest classes from the repository
https://github.com/zxing/zxing/tree/master/android-integration/src/main/java/com/google/zxing/integration/android
See the javadoc of the class to see how to use it. First add code to invoke the Intent:
IntentIntegrator integrator = new IntentIntegrator(yourActivity);
integrator.initiateScan();
Second, add this to your Activity to handle the result:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null) {
// handle scan result
}
// else continue with any other code you need in the method
}
More info here https://github.com/zxing/zxing/wiki/Scanning-Via-Intent

Related Links

android widget or app for reliable repeated tasks
Line by line output in the ListView with ArrayAdapter
Android: Change text color/background also for buttons?
My webview doesn't open in my second activity
Get my telephone number programatically on android
Google Maps Marker not getting displayed
How to create multiple pages on the server with socket?
How to display Google Map in fragment?
How to track GPS If that Reaches particular location or around 20 meters?
Cordova Dynamic Zoom & Meta Viewport Not Working
android service to show push notification on launch of an application
Open different activities from listview
input width ignored on android device
Converting Camera Coordinates to Custom View Coordinates
Geolocation API coordinates incorrect for CDMA cellid
Maintaining a ListView where each List item has a one to many relationship

Categories

HOME
service-worker
phantom-dsl
oracle12c
design-patterns
interface
requirements
switch-statement
dojo
limit
bookmarks
jax-ws
bing-maps
qpython
sendgrid-api-v3
32bit-64bit
google-chrome-app
github-enterprise
pega
activecollab
why3
emoji
enterprise-architect
plink
symbol
scsi
mathprog
feathersjs
production
sencha-touch
mongoid6
flooding
jasmine-node
underscore.js
fractions
firebase-analytics
custom-controls
autodesk-designautomation
gitlab-api
utorrent
glib
biztalk-rule-engine
vue-chartjs
jwplayer6
getlasterror
xcode7.3
zoomify
dm-script
android-augmented-reality
comparison-operators
resolution
magento-1.9.3
escpos
parser-generator
sharp-snmp
apache-spark-dataset
opencmis
xbmc
textsum
lampp
arq
ngmaterial
cpu-cache
google-photos
shinobi
gets
chromium-os
slackware
node-request
greatest-n-per-group
beamer
hclust
observablecollection
shopping
phpwebsocket
libvlc
linklabel
decimalformat
tfs-workitem
avisynth
oracle-spatial
lasso
ansi
fail2ban
user-permissions
theos
rickshaw
mdichild
named-parameters
ss7
malformedurlexception
maven-scm
inputmismatchexception
time.h
mantle
server-administration
cascadingdropdown
android-sdk-2.3
device-width
os.system
correlated-subquery
floating-point-exceptions
catransform3d
cuteeditor
django-paypal
appledoc
uideviceorientation
updatesourcetrigger
zend-rest
murmurhash
purepdf
linfu-dynamicproxy
httpcookie
pbcopy
mathematical-typesetting

Resources

Encrypt Message