The Project 3
Improve the Alexandria part
How to use Barcode Reader(ZXing)
compile 'com.google.zxing:android-core:3.2.1'
using google mobile vision?
finally method:
we will call the already install app to scanner the barcode
this is the most easy way to make the app can scan the barcode
basic knowledge:
how to start google play to download a specific app?
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + "com.google.zxing.client.android")));
"com.google.zxing.client.android" is the App PKG
After click the button call intaleed zxing app
rootView.findViewById(R.id.scan_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// This is the callback method that the system will invoke when your button is
// clicked. You might do this by launching another app or by including the
//functionality directly in this app.
// Hint: Use a Try/Catch block to handle the Intent dispatch gracefully, if you
// are using an external app.
//when you're done, remove the toast below.
activity = getActivity();
// CharSequence text = "This button should let you scan a book for its barcode!";
// int duration = Toast.LENGTH_SHORT;
//
// Toast toast = Toast.makeText(context, text, duration);
// toast.show();
try{
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.setPackage("com.google.zxing.client.android");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
catch (Exception e){
// not install the zxing
// go to the market download
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + "com.google.zxing.client.android")));
}
}
});
handle the ZXing return
1.override onActivityResult method
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == BARCODE_SCANNER_REQUEST_CODE) {
//only for the ZXing app
if (resultCode == activity.RESULT_OK) {
String contents = data.getStringExtra("SCAN_RESULT");
String format = data.getStringExtra("SCAN_RESULT_FORMAT");
Log.d("onActivityResult", format + " : " + contents);
ean.setText(contents);
} else if (resultCode == activity.RESULT_CANCELED) {
// Handle cancel
Toast.makeText(activity,"Scan cancel",Toast.LENGTH_SHORT).show();
}
}
}
Another Problem
//if the download is very slow,than the cursor loader may not can load the data in the SQL database
//so,when the service finish,send a brodcast message ,than the brodcast receiver start the cursor loader in on receive
//register the Receiver in add book fragment
MyBrodcastReceiver mybrodcastReceiver=new MyBrodcastReceiver();
IntentFilter intentFilter=new IntentFilter(BRODCAST_ACTION);
getActivity().registerReceiver(mybrodcastReceiver,intentFilter);
//in BookServie send brodcast
if (bookEntry.getCount() > 0) {
bookEntry.close();
//Todo send brodcast
return;
}
//and
//finish inset the data into the SQL
//the send Brodcast cod is.
private void sendBrodcasttoStarttheCursorLoader(){
Intent brodcastIntent=new Intent();
brodcastIntent.setAction(AddBook.BRODCAST_ACTION);
sendBroadcast(brodcastIntent);
}
//in OnReceive,start the cursor loader
private class MyBrodcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//when receive the message(Service finish),start the Cursor Loader.
AddBook.this.restartLoader();
}
}
Extra problem 2!!!!!!!!!!!!!
//when cant show the picture,the layout has some problem
//because of the relative layout
android:layout_height="wrap_content" to 70dp
because of the
<ImageView
android:layout_width="wrap_content"
android:layout_height="70dp"
android:id="@+id/fullBookCover"
android:contentDescription="@string/book_cover"
android:layout_marginTop="15dp"
android:layout_below="@+id/fullBookTitle"
android:layout_alignLeft="@+id/fullBookTitle"
android:layout_alignStart="@+id/fullBookTitle"/>