将QR码扫描仪放入片段中

时间:2013-04-16 03:09:26

标签: android android-fragments qr-code

我正在使用Android QR码处理库 - android QR code。  我正在为扫描程序扩展DecoderActivity,现在我希望扫描程序位于片段内,我使用LocalActivityManager将Activity嵌入片段中。这是代码:

public class QrCodeProcessorFragment extends SherlockFragment {

private static final String KEY_STATE_BUNDLE = "localActivityManagerState";

private LocalActivityManager mLocalActivityManager;

Button generate_qr_code;

private QuickPayManagerActivity parent;

protected LocalActivityManager getLocalActivityManager() {
    return mLocalActivityManager;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle state = null;
    if (savedInstanceState != null) {
        state = savedInstanceState.getBundle(KEY_STATE_BUNDLE);
    }

    mLocalActivityManager = new LocalActivityManager(getActivity(), true);
    mLocalActivityManager.dispatchCreate(state);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // This is where you specify you activity class
    Intent i = new Intent(getSherlockActivity(), CaptureActivity.class);
    Window w = mLocalActivityManager.startActivity("tag", i);
    View currentView = w.getDecorView();
    ViewGroup vg = (ViewGroup) (currentView.getParent());
    if (vg != null)
        vg.removeView(currentView);

    currentView.setVisibility(View.VISIBLE);
    currentView.setFocusableInTouchMode(true);
    ((ViewGroup) currentView)
            .setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

    return currentView;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBundle(KEY_STATE_BUNDLE,
            mLocalActivityManager.saveInstanceState());
}

@Override
public void onResume() {
    System.out.println("lam onresume");
    super.onResume();
    mLocalActivityManager.dispatchResume();
}

@Override
public void onPause() {
    System.out.println("lam onpause");
    super.onPause();
    mLocalActivityManager.dispatchPause(getActivity().isFinishing());
}

@Override
public void onStop() {
    super.onStop();
    mLocalActivityManager.dispatchStop();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mLocalActivityManager.dispatchDestroy(getActivity().isFinishing());
}}

QR扫描仪现在无法使用。我调试了库代码,它期待一个活动实例,我正在传递片段内部的活动实例。希望我对此很清楚。请帮忙!

1 个答案:

答案 0 :(得分:1)

我找到了一个名为Barcode Fragment library的库,它使用片段来托管扫描仪功能。工作正常,但不支持纵向模式支持,我已根据here中的建议对库进行了更改。它就像一个魅力:)