android – 已恢复.取消后的计时器给出“java.lang.IllegalStateException:Timer cancelled”.

前端之家收集整理的这篇文章主要介绍了android – 已恢复.取消后的计时器给出“java.lang.IllegalStateException:Timer cancelled”.前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想知道我取消后是否可以再次安排计时器.这里有人说,http://www.coderanch.com/t/452066/java/java/Exception-timer-IllegalStateException,一旦你取消了计时器,就不可能在同一个计时器上安排任何东西,除非我创建一个新的计时器.而且,我遇到以下错误,java.lang.IllegalStateException:计时器被取消.
我正试图推出一款定期执行Wifi扫描的定时器.但我希望“暂停”或者如果没有,在扫描后执行某些计算时取消定时器.只有在完成计算并返回一些结果后,我才会恢复计时器.谁能告诉我如何解决这个问题呢?

忘了提到我只有在我完成加载图像之后我才会启动计时器,我使用AsyncTask来做到这一点.

我试图实现这个单独的线程,以便它不会阻止UI线程.

这是程序和启动计时器的粗略骨架,只有在图像完全加载后才会执行wifi扫描(在“load.execute(context);”之后):

public class LargeImageScroller extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(new SampleView(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {...}

@Override
public boolean onOptionsItemSelected(MenuItem item) {...}

//this is the class where the program would do all the UI and display images
private static class SampleView extends View {

    public SampleView(Context context) {
                    :
        loadMap load = new loadMap();
        load.execute(context);

        scanTask = new TimerTask(){

            @Override
            public void run() {
                // TODO Auto-generated method stub
               handler.post(new Runnable() {
                        public void run() {
                         wifi = (WifiManager)context.getSystemService(WIFI_SERVICE);
                         context.registerReceiver(receiverWifi,new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
                         wifi.startScan();
                         Log.d("TIMER","Timer set off");
                        }
               });
            }

        };

        scanTimer.schedule(scanTask,refreshRate);
    }

    public class wifiReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context,Intent intent) {
            // TODO Auto-generated method stub
            List<ScanResult> sc = wifi.getScanResults(); 
            for(int i=0; i<sc.size(); i++){
                Log.e("AndroidRuntime",sc.get(i).SSID);
            }
        }

    }
    public boolean onTouchEvent(MotionEvent event) {...}
    protected void onDraw(Canvas canvas) {...}
    private static Drawable LoadImageFromWebOperations(String url){...}
    private static Bitmap decodeFile(File f,int requiredSize){...}
    private class loadMap extends AsyncTask<Context,Void,ArrayList<Bitmap>>{...}
}

使用包含的权限:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.DELETE_CACHE_FILES"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

广播接收器遇到错误

10-25 05:53:04.903: ERROR/ActivityThread(1551): Activity android.wps.LargeImageScroller has leaked IntentReceiver android.wps.LargeImageScroller$SampleView$wifiReceiver@43d1bca0 that was originally registered here. Are you missing a call to unregisterReceiver()?
10-25 05:53:04.903: ERROR/ActivityThread(1551): android.app.IntentReceiverLeaked: Activity android.wps.LargeImageScroller has leaked IntentReceiver android.wps.LargeImageScroller$SampleView$wifiReceiver@43d1bca0 that was originally registered here. Are you missing a call to unregisterReceiver()?
10-25 05:53:04.903: ERROR/ActivityThread(1551):     at android.app.ActivityThread$PackageInfo$ReceiverDispatcher.<init>(ActivityThread.java:797)
10-25 05:53:04.903: ERROR/ActivityThread(1551):     at android.app.ActivityThread$PackageInfo.getReceiverDispatcher(ActivityThread.java:608)
10-25 05:53:04.903: ERROR/ActivityThread(1551):     at android.app.ApplicationContext.registerReceiverInternal(ApplicationContext.java:724)
10-25 05:53:04.903: ERROR/ActivityThread(1551):     at android.app.ApplicationContext.registerReceiver(ApplicationContext.java:711)
10-25 05:53:04.903: ERROR/ActivityThread(1551):     at android.app.ApplicationContext.registerReceiver(ApplicationContext.java:705)
10-25 05:53:04.903: ERROR/ActivityThread(1551):     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:308)
10-25 05:53:04.903: ERROR/ActivityThread(1551):     at android.wps.LargeImageScroller$SampleView$1$1.run(LargeImageScroller.java:187)
10-25 05:53:04.903: ERROR/ActivityThread(1551):     at android.os.Handler.handleCallback(Handler.java:587)
10-25 05:53:04.903: ERROR/ActivityThread(1551):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-25 05:53:04.903: ERROR/ActivityThread(1551):     at android.os.Looper.loop(Looper.java:123)
10-25 05:53:04.903: ERROR/ActivityThread(1551):     at android.app.ActivityThread.main(ActivityThread.java:4363)
10-25 05:53:04.903: ERROR/ActivityThread(1551):     at java.lang.reflect.Method.invokeNative(Native Method)
10-25 05:53:04.903: ERROR/ActivityThread(1551):     at java.lang.reflect.Method.invoke(Method.java:521)
10-25 05:53:04.903: ERROR/ActivityThread(1551):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-25 05:53:04.903: ERROR/ActivityThread(1551):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-25 05:53:04.903: ERROR/ActivityThread(1551):     at dalvik.system.NativeStart.main(Native Method)

解决方法

是的,取消计时器会终止其线程,因此您无法再次使用它.计时器没有任何内置的暂停方法.当您想要“暂停”时可以取消定时器,并在想要“恢复”时再创建一个新定时器.
原文链接:https://www.f2er.com/android/316956.html

猜你在找的Android相关文章