完成的设备扫描项目的几个关键程序,包括activity之间的转换
Posted strongdady
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了完成的设备扫描项目的几个关键程序,包括activity之间的转换相关的知识,希望对你有一定的参考价值。
module 的 gradle.build
最后三行的compile 是关键
dependencies {
implementation fileTree(dir: ‘libs‘, include: [‘*.jar‘])
implementation ‘com.android.support:appcompat-v7:26.1.0‘
implementation ‘com.android.support:design:26.1.0‘
implementation ‘com.android.support.constraint:constraint-layout:1.1.0‘
implementation ‘com.android.support:support-vector-drawable:26.1.0‘
testImplementation ‘junit:junit:4.12‘
androidTestImplementation ‘com.android.support.test:runner:1.0.2‘
androidTestImplementation ‘com.android.support.test.espresso:espresso-core:3.0.2‘
compile ‘com.journeyapps:zxing-android-embedded:[email protected]‘
compile ‘com.journeyapps:zxing-android-legacy:[email protected]‘
compile ‘com.journeyapps:zxing-android-integration:[email protected]‘
}
/******************************************/
inputActivity.java
package com.strongdady.devicescan;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.database.Cursor;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import static android.os.Environment.getExternalStorageDirectory;
public class inputActivity extends Activity {
public static String str=null;
protected void onCreate(Bundle savedInstanceState) {
final Button buttonExit,buttonCheck;
final EditText editText0;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input);
editText0 = (EditText) findViewById(R.id.sample_edit_text0);
buttonCheck = (Button) findViewById(R.id.sample_buttonCheck);
buttonExit= (Button) findViewById(R.id.sample_buttonExit);
buttonCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i=new Intent();
i.putExtra("device_no", editText0.getText().toString());
setResult(4,i);
finish();
}
});
buttonExit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i=new Intent();
i.putExtra("device_no", editText0.getText().toString());
setResult(0,i);
finish();
}
});
}
}
/*************MainActivity.java*************/
package com.strongdady.devicescan;
import android.os.Process;
import java.util.Arrays;
import java.util.List;
import java.io.File;
import java.io.InputStream;
import java.io.FileOutputStream;
import android.database.sqlite.SQLiteDatabase;
import android.database.Cursor;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import android.os.Environment;
import android.widget.Toast;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;
import static android.os.Environment.getExternalStorageDirectory;
public class MainActivity extends AppCompatActivity {
private final int BUFFER_SIZE = 400000;
public static final String DB_NAME = "device.db"; // 保存的数据库文件名称
public static final String PACKAGE_NAME = "com.strongdady.qrscan";// 应用的包名
//public static final String DB_PATH = "/data"droid.os.Environment.getDataDirectory().getAbsolutePath()+"/"
public static final String DB_PATH =getExternalStorageDirectory()+"/data";
String device_no,device_name, owner, register_date,device_nanm,sqlString;
SQLiteDatabase db=null;
Cursor cursor;
private TextView mTextMessage;
public void copyDBFile()
{
try {
File myDataPath = new File(DB_PATH+"/device_db");
if (!myDataPath.exists())
{
myDataPath.mkdirs();// 假设没有这个文件夹,则创建
}
String dbfile=myDataPath+"/"+DB_NAME;
if (!(new File(dbfile).exists())) {// 推断数据库文件是否存在,若不存在则运行导入,否则直接打开数据库
InputStream is =getResources().openRawResource(R.raw.device); // 欲导入的数据库
FileOutputStream fos = new FileOutputStream(dbfile);
byte[] buffer = new byte[BUFFER_SIZE];
int count = 0;
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
db = SQLiteDatabase.openOrCreateDatabase(dbfile,null);
} catch (Exception e)
{
mTextMessage.append(e.toString());
}
}
public Device findDeviceInfo(String qrCode)
{
String device_no=null,device_name=null,device_owner=null,device_registerDate=null;
Device device1;
//qrCode.trim()去掉前后空格
cursor = db.rawQuery("SELECT * FROM device where device_no = ‘"+qrCode.trim()+"‘", null);
while (cursor.moveToNext()) {
device_no = qrCode;
device_name = cursor.getString(cursor.getColumnIndex("owner"));
device_owner = cursor.getString(cursor.getColumnIndex("device_name"));
device_registerDate = cursor.getString(cursor.getColumnIndex("register_date"));
register_date=cursor.getString(cursor.getColumnIndex("register_date"));
}
if( device_name!=null){
device1 = new Device();
device1.Name = device_name;
device1.no = device_no;
device1.Owner = device_owner;
device1.RegisterDate = register_date;
return device1;
}else{
return null;
}
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
//mTextMessage.setText(R.string.title_home);
mTextMessage.setText("");
IntentIntegrator integrator=new IntentIntegrator(MainActivity.this);
List<String> oDesiredFormats = Arrays.asList("PDF_417,UPC_A,UPC_E,EAN_13,EAN_8,RSS_14,RSS_EXPANDED,CODE_39,CODE_93,CODE_128,ITF,CODABAR,QR_CODE,DATA_MATRIX".split(","));
//integrator.setOrientation()
//integrator.setBeepEnabled(true);
//integrator.setOrientation(0);
//integrator.setPrompt("scaning .....");
integrator.initiateScan(oDesiredFormats);
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
startActivityForResult(new Intent(MainActivity.this,
inputActivity.class), 1);
return true;
case R.id.navigation_notifications:
cursor.close();
db.close();
finish();
System.exit(0);
Process.killProcess(Process.myPid());
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
copyDBFile();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult scanResult=null;
super.onActivityResult(requestCode, resultCode, data);
String qrCode = null;
try {
scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
}catch (Exception e){
mTextMessage.append("\nException occured during scaning :"+e+"\n");
}
if (requestCode == 1 && resultCode == 0) {
return;
}
if (requestCode == 1 && resultCode == 4) {
String s = data.getStringExtra("device_no");
qrCode = s;
Device device1 = findDeviceInfo(qrCode);
if (device1 != null) {
mTextMessage.setText("\n 设备编号: " + device1.no + "\n 设备名称:" + device1.Name + "\n 责任人:" + device1.Owner + " " + "\n 登记日期:" + device1.RegisterDate);
} else {
mTextMessage.setText("\n 找不到编号为:" + qrCode + "的设备");
}
} else {
qrCode=scanResult.getContents().toString();
if (scanResult != null) {
Device device1 = findDeviceInfo(qrCode);
if (device1 != null) {
mTextMessage.setText("\n 设备编号: " + device1.no + "\n 设备名称:" + device1.Name + "\n 责任人:" + device1.Owner + " " + "\n 登记日期:" + device1.RegisterDate);
} else {
mTextMessage.setText("\n !!!: 找不到编号为:" + qrCode + "的设备");
}
} else {
//super.onActivityResult(requestCode, resultCode, data);
//mTextMessage.setText(" 找不到编号为:"+qrCode+"的设备");
mTextMessage.setText("\n找不到编号为:" + qrCode + "的设备");
}
}
}
}
/*********************/
Device.java
package com.strongdady.devicescan;
public class Device {
public String no, Owner,Name,RegisterDate;
}
以上是关于完成的设备扫描项目的几个关键程序,包括activity之间的转换的主要内容,如果未能解决你的问题,请参考以下文章