我在Android上使用条形码扫描仪时遇到问题。我尝试构建一个应用程序,其中有一个按钮,该按钮运行新活动,扫描代码,然后使用扫描的代码返回到 MainActivity 。但是...
这是 MainActivity 的代码:
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new ScanFragment()).commit();
navigationView.setCheckedItem(R.id.nav_scan);
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_scan:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new ScanFragment()).commit();
break;
case R.id.nav_okruh:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new OkruhFragment()).commit();
break;
case R.id.nav_kzavozu:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new KZavozuFragment()).commit();
break;
case R.id.nav_zprava:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new ZpravaFragment()).commit();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
它是带有fragments
的抽屉式布局。每个片段都用于其他操作,并且在第一个操作中,有一个button
:
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.google.android.gms.vision.barcode.BarcodeDetector;
import static com.google.android.gms.vision.barcode.Barcode.ALL_FORMATS;
public class ScanFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_scan, container, false);
Button button = (Button) view.findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ScanOkno.class);
startActivity(intent);
}
});
return view;
}
}
这是扫描仪的活动,用名为ScanOkno.class
的代码表示:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.SparseArray;
import android.widget.Toast;
import com.google.android.gms.vision.barcode.Barcode;
import java.util.List;
import info.androidhive.barcode.BarcodeReader;
public class ScanOkno extends AppCompatActivity implements BarcodeReader.BarcodeReaderListener {
private BarcodeReader barcodeReader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan_okno);
barcodeReader = (BarcodeReader) getSupportFragmentManager().findFragmentById(R.id.barcode_fragment);
}
@Override
public void onScanned(Barcode barcode) {
// play beep sound
barcodeReader.playBeep();
barcodeReader.pauseScanning();
finish();
}
@Override
public void onScannedMultiple(List<Barcode> list) {
}
@Override
public void onBitmapScanned(SparseArray<Barcode> sparseArray) {
}
@Override
public void onScanError(String s) {
}
@Override
public void onCameraPermissionDenied() {
Toast.makeText(getApplicationContext(), "Camera permission denied!", Toast.LENGTH_LONG).show();
}
}
我的问题是finish()
;函数在代码的onScanned()
部分中起作用,因为它不会关闭活动ScanOkno
。窗口在代码周围显示框架,如果被扫描,它会发出哔哔声,然后我暂停扫描并想结束活动,但是我找不到原因,为什么它不想关闭并返回到MainActivity
。
我也不知道如何将扫描的代码转移到主要活动中,但是我认为,可以使用诸如final code = barcode.rawValue;
感谢您的回复。
答案 0 :(得分:0)
没有足够的代表来评论,但是在主线程上调用了此方法吗?
version: "2.4"
services:
nginx:
image: nginx:latest
ports:
- "80:80"
depends_on:
- AService
networks:
- test-network
AService:
image: ubuntu:latest
ports:
- "80:80"
depends_on:
- BService
networks:
- test-network
.....
networks:
test-network:
name: ctest-network
ipam:
driver: default
config:
- subnet: 10.10.10.0/24
如果从后台线程中触发,@Override
public void onScanned(Barcode barcode) {
// play beep sound
barcodeReader.playBeep();
barcodeReader.pauseScanning();
finish();
}
可能会被忽略(不过,似乎找不到任何文档说明如果从其他地方调用它会发生什么情况。)
答案 1 :(得分:-1)
更新
感谢纠正我@Gabe Sechan
从您MainActivity
致电ScanOkno
到startActivityForResult
:
startActivityForResult(ScanOkno.class, REQUEST_CODE);
并将结果发送回MainActivity:
Intent resultIntent = new Intent();
// passing data back to result
resultIntent.putExtra("code", "some_string");
setResult(Activity.RESULT_OK, resultIntent);
finish();
并接收结果:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
String code = data.getStringExtra("code");
}
}