-1

我正在使用一个TimerTask()应用previewCallback程序。但是,它停止工作。我是 Android 开发的新手,我不知道问题出在哪里或在哪里。我试图在 5 秒后响应相机预览。我在不使用计时器的情况下在函数中尝试了相同的代码行,它if( isQrCodeScanned )可以phoneCamera.autoFocus(autoFocusCallback);工作。 onClick()当我点击按钮时,相机预览被重新激活。但是,对于TimerTask(),它崩溃了。

private Timer timer;
.
.
.
timer.schedule( new TimerTask()
           {

            @Override
            public void run()
            {

               if( isQrCodeScanned )
               { 
                  isQrCodeScanned = false;
                  isPreviewing = true;                      
                  qrCodeText.setText( "Waiting for a QR Code..." );

                  phoneCamera.setPreviewCallback(previewCallback);
                  phoneCamera.startPreview();
                  phoneCamera.autoFocus(autoFocusCallback);
               }

            }}, ACTIVATE_LIMIT);

谢谢您的帮助!

日志猫:

03-03 13:04:42.927: W/Vold(29): No UMS switch available
03-03 13:04:44.357: E/Zygote(33): setreuid() failed. errno: 2
03-03 13:04:45.137: W/MediaProfiles(33): could not find media config xml file
03-03 13:04:50.137: E/Zygote(33): setreuid() failed. errno: 17
03-03 13:04:51.037: E/BatteryService(59): usbOnlinePath not found
03-03 13:04:51.037: E/BatteryService(59): batteryVoltagePath not found
03-03 13:04:51.037: E/BatteryService(59): batteryTemperaturePath not found
03-03 13:04:51.056: E/SurfaceFlinger(59): Couldn't open /sys/power/wait_for_fb_sleep or /sys/power/wait_for_fb_wake
03-03 13:04:51.557: W/zipro(69): Unable to open zip '/data/local/bootanimation.zip': No such file or directory
03-03 13:04:51.557: W/zipro(69): Unable to open zip '/system/media/bootanimation.zip': No such file or directory
03-03 13:04:52.457: W/PackageManager(59): Running ENG build: no pre-dexopt!
03-03 13:04:54.607: W/PackageParser(59): No actions in intent filter at /data/app/ApiDemos.apk Binary XML file line #1841
03-03 13:04:54.617: W/PackageParser(59): No actions in intent filter at /data/app/ApiDemos.apk Binary XML file line #1847
03-03 13:04:54.627: W/PackageManager(59): Package com.example.android.apis desires unavailable shared library com.example.will.never.exist; ignoring!
03-03 13:04:54.678: W/PackageManager(59): Unknown permission com.google.android.googleapps.permission.GOOGLE_AUTH.mail in package com.android.contacts
03-03 13:04:54.678: W/PackageManager(59): Unknown permission android.permission.ADD_SYSTEM_SERVICE in package com.android.phone
03-03 13:04:54.678: W/PackageManager(59): Not granting permission android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS to package com.android.browser (protectionLevel=2 flags=0x1be45)
03-03 13:04:54.678: W/PackageManager(59): Unknown permission com.google.android.gm.permission.WRITE_GMAIL in package com.android.settings
03-03 13:04:54.678: W/PackageManager(59): Unknown permission com.google.android.gm.permission.READ_GMAIL in package com.android.settings
03-03 13:04:54.678: W/PackageManager(59): Unknown permission com.google.android.googleapps.permission.GOOGLE_AUTH in package com.android.settings
03-03 13:04:54.689: W/PackageManager(59): Unknown permission com.google.android.googleapps.permission.GOOGLE_AUTH in package com.android.providers.contacts
03-03 13:04:54.689: W/PackageManager(59): Unknown permission com.google.android.googleapps.permission.GOOGLE_AUTH.cp in package com.android.providers.contacts
03-03 13:04:54.689: W/PackageManager(59): Unknown permission com.google.android.googleapps.permission.ACCESS_GOOGLE_PASSWORD in package com.android.development
03-03 13:04:54.689: W/PackageManager(59): Unknown permission com.google.android.googleapps.permission.GOOGLE_AUTH in package com.android.development
03-03 13:04:54.689: W/PackageManager(59): Unknown permission com.google.android.googleapps.permission.GOOGLE_AUTH.ALL_SERVICES in package com.android.development
03-03 13:04:54.689: W/PackageManager(59): Unknown permission com.google.android.googleapps.permission.GOOGLE_AUTH.YouTubeUser in package com.android.development
03-03 13:04:55.407: E/EventHub(59): could not get driver version for /dev/input/mouse0, Not a typewriter
03-03 13:04:55.407: E/EventHub(59): could not get driver version for /dev/input/mice, Not a typewriter
03-03 13:04:56.017: E/System(59): Failure starting core service
03-03 13:04:56.017: E/System(59): java.lang.SecurityException
03-03 13:04:56.017: E/System(59):   at android.os.BinderProxy.transact(Native Method)
03-03 13:04:56.017: E/System(59):   at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)
03-03 13:04:56.017: E/System(59):   at android.os.ServiceManager.addService(ServiceManager.java:72)
03-03 13:04:56.017: E/System(59):   at com.android.server.ServerThread.run(SystemServer.java:184)
03-03 13:04:56.557: W/AudioPolicyManagerBase(34): setPhoneState() setting same state 0
03-03 13:04:56.577: E/SoundPool(59): error loading /system/media/audio/ui/Effect_Tick.ogg
03-03 13:04:56.577: W/AudioService(59): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
03-03 13:04:56.577: E/SoundPool(59): error loading /system/media/audio/ui/KeypressStandard.ogg
03-03 13:04:56.577: E/SoundPool(59): error loading /system/media/audio/ui/KeypressSpacebar.ogg
03-03 13:04:56.577: W/AudioService(59): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
03-03 13:04:56.577: E/SoundPool(59): error loading /system/media/audio/ui/KeypressDelete.ogg
03-03 13:04:56.577: W/AudioService(59): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
03-03 13:04:56.577: E/SoundPool(59): error loading /system/media/audio/ui/KeypressReturn.ogg
03-03 13:04:56.577: W/AudioService(59): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
03-03 13:04:56.587: W/HeadsetObserver(59): This kernel does not have wired headset support
03-03 13:04:56.587: W/DockObserver(59): This kernel does not have dock station support
03-03 13:04:56.697: W/DevicePolicyManagerService(59): failed parsing /data/system/device_policies.xml java.io.FileNotFoundException: /data/system/device_policies.xml (No such file or directory)
03-03 13:04:56.947: W/StatusBar(59): No icon ID for slot ime
03-03 13:04:57.327: W/RecognitionManagerService(59): no available voice recognition services found
03-03 13:04:57.498: E/ThrottleService(59): Could not open GPS configuration file /etc/gps.conf
03-03 13:04:57.637: W/GpsLocationProvider(59): Could not open GPS configuration file /etc/gps.conf
03-03 13:04:57.727: W/ActivityManager(59): Unable to start service Intent { act=@0 }: not found
03-03 13:04:57.727: W/ActivityManager(59): Unable to start service Intent { act=@0 }: not found
03-03 13:04:58.607: E/logwrapper(148): executing /system/bin/tc failed: No such file or directory
03-03 13:04:58.647: E/logwrapper(149): executing /system/bin/tc failed: No such file or directory
03-03 13:04:58.757: E/logwrapper(150): executing /system/bin/tc failed: No such file or directory
03-03 13:05:01.617: W/ActivityManager(59): Unable to start service Intent { act=com.android.ussd.IExtendedNetworkService }: not found
03-03 13:05:00.774: W/SystemClock(112): Unable to set rtc to 1362315900: Invalid argument
03-03 13:05:05.370: E/HierarchicalStateMachine(59): TetherMaster - unhandledMessage: msg.what=3
03-03 13:05:14.851: W/PackageManager(59): Unknown permission com.google.android.googleapps.permission.GOOGLE_AUTH.mail in package com.android.contacts
03-03 13:05:14.851: W/PackageManager(59): Unknown permission android.permission.ADD_SYSTEM_SERVICE in package com.android.phone
03-03 13:05:14.851: W/PackageManager(59): Not granting permission android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS to package com.android.browser (protectionLevel=2 flags=0x1be45)
03-03 13:05:14.860: W/PackageManager(59): Unknown permission com.google.android.gm.permission.WRITE_GMAIL in package com.android.settings
03-03 13:05:14.870: W/PackageManager(59): Unknown permission com.google.android.gm.permission.READ_GMAIL in package com.android.settings
03-03 13:05:14.870: W/PackageManager(59): Unknown permission com.google.android.googleapps.permission.GOOGLE_AUTH in package com.android.settings
03-03 13:05:14.870: W/PackageManager(59): Unknown permission com.google.android.googleapps.permission.GOOGLE_AUTH in package com.android.providers.contacts
03-03 13:05:14.870: W/PackageManager(59): Unknown permission com.google.android.googleapps.permission.GOOGLE_AUTH.cp in package com.android.providers.contacts
03-03 13:05:14.870: W/PackageManager(59): Unknown permission com.google.android.googleapps.permission.ACCESS_GOOGLE_PASSWORD in package com.android.development
03-03 13:05:14.881: W/PackageManager(59): Unknown permission com.google.android.googleapps.permission.GOOGLE_AUTH in package com.android.development
03-03 13:05:14.881: W/PackageManager(59): Unknown permission com.google.android.googleapps.permission.GOOGLE_AUTH.ALL_SERVICES in package com.android.development
03-03 13:05:14.881: W/PackageManager(59): Unknown permission com.google.android.googleapps.permission.GOOGLE_AUTH.YouTubeUser in package com.android.development
03-03 13:05:17.520: W/RecognitionManagerService(59): no available voice recognition services found

这是错误代码的全部部分previewCallback

PreviewCallback previewCallback = new PreviewCallback()
    {
        public void onPreviewFrame( byte[] data, Camera camera )
        {
            .
            .
            .

            if( scanResult != 0 )
            {
                isPreviewing = false;
                phoneCamera.setPreviewCallback( null );
                phoneCamera.stopPreview();

                SymbolSet qrCodeContent = scanner.getResults();

                for( Symbol symbol : qrCodeContent )
                {
                    qrCodeText.setText("QR CODE CONTAINS: " + symbol.getData() );
                    isQrCodeScanned = true;
                }

                timer.schedule( new TimerTask()
                                {

                                    @Override
                                    public void run()
                                    {
                                        // TODO Auto-generated method stub

                                        if( isQrCodeScanned )
                                        {

                                            isQrCodeScanned = false;
                                            isPreviewing = true;

                                            qrCodeText.setText( "Waiting for a QR Code..." );

                                            phoneCamera.setPreviewCallback(previewCallback);
                                            phoneCamera.startPreview();
                                            phoneCamera.autoFocus(autoFocusCallback);
                                        }

                                    }

                                }, ACTIVATE_LIMIT);

            }

        }

    }; // End of previewCallback
4

1 回答 1

0

A TimerTask runs on a separate thread and can't change things in the UI-thread. Maybe it would work if you'd run the code on the ui thread? For example:

timer.schedule( new TimerTask() {
    @Override
    public void run() {
        if( isQrCodeScanned ) { 
            isQrCodeScanned = false;
            isPreviewing = true;                      
            runOnUiThread(new Runnable() {
                public void run() {
                    qrCodeText.setText( "Waiting for a QR Code..." );
                    phoneCamera.setPreviewCallback(previewCallback);
                    phoneCamera.startPreview();
                    phoneCamera.autoFocus(autoFocusCallback);
                }
           });
       }
   }
}, ACTIVATE_LIMIT);

Haven't tested this. Hope it helps.

Cheers, Martijn.

于 2013-03-03T13:38:47.667 回答