重新安排。取消后的计时器给出“java.lang.IllegalStateException:计时器已取消。”

发布于 2024-12-11 08:10:31 字数 5728 浏览 0 评论 0原文

我想知道取消后是否可以再次安排计时器。此处声明, 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)

i would like to know whether i can schedule the timer again after i cancelled it. It was stated here, http://www.coderanch.com/t/452066/java/java/Exception-timer-IllegalStateException, that once you cancelled the timer, it would not be possible to scheduling anything on the same timer unless i create a new Timer. And also, i encountered the following error, java.lang.IllegalStateException: Timer was cancelled.
I am trying to come out with a Timer that would regularly perform Wifi scanning. But i wished to "pause" or if not, cancelled the timer when it is performing some calculation after scanning. Only then after it complete the calculation and return some results back, i would resume the Timer. Can anyone tell me how i can go about in getting this problem solved?

Forgotten to mentioned that i would start the timer only after i finished loading images which i used AsyncTask to do that.

I trying to achieved this one a separate thread so that it would not hold up the UI thread.

Here is the rough skeleton of the program and start timer where it would perform wifi scanning when time elasped only after the image have been loaded completely (after "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 that was included:

<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>

Error encounter for the Broadcast receiver:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱已欠费 2024-12-18 08:10:31

是的,取消计时器会终止其线程,因此您无法再次使用它。计时器没有任何内置的暂停方法。当您想“暂停”时,您可以取消计时器;当您想“恢复”时,您可以创建一个新计时器。

Yeah, cancelling the timer terminates its thread so you can't use it again. The timer doesn't have any built-in methods for pausing. You can cancel the timer when you want to "pause" and make a new one when you want to "resume".

土豪 2024-12-18 08:10:31

终于解决了:D
我没有时间解释,但我想与你分享。

import java.util.Timer;  import java.util.TimerTask;  import sociatag.view.main_frame;  public class devices_pinger implements Runnable {




// PROPERTIES:

private int delay = 1000;   // delay for 1 seconds.
private int period = 6000;  // repeat every 6 seconds.
private boolean stop_timer = false;
// CONSTRACTOR:

public devices_pinger() {
}
// METHODES:

/*
 * because we implements Runnable in this class,
 * this methode run() has to run this class on a seperate thread.
 * - call the start pinger methode
 *
 * @param: event occured on the serial
 */
@Override
public void run() {
    run_pinger();

}

/*
 * this is the start point of this class
 * - create a scheduler to run every x seconds
 * - call the run methode every x seconds to:
 * - send 'P' through the serial
 * - wait x seconds
 * - call the ping analyser to get all the replyed pings and analyse them
 */
private void run_pinger() {

    new Timer().schedule(new TimerTask() {

        @Override
        public void run() {

            // 1) ping the connected device
            serial_connection.serial_write('P');
            System.out.println("> PING (Send P)");//<------TESTING--------------------------------------------
            // pause for 3 seonds to give time for all the devices to response 
            try {
                Thread.sleep(3000); // call the analyser after 3 seconds
            } catch (InterruptedException ex) {
                System.out.println("Error: while pausing the thread");
            } // 2) get the result of the ping to analyze it
            serial_listener.ping_analyser();

            if (stop_timer == true) {
                this.cancel();
            }
        }
    }, delay, period);
}

/*
 * stop the timer before pairing the devices or do other activities
 */
public void stop_pinger() {

    stop_timer = true;
    main_frame.display_notification("Device Pinger is paused.");
}

/*
 * restart the pinger by creating new instance of timer
 */
public void start_pinger() {
    stop_timer = false;
    run_pinger();
    main_frame.display_notification("Device Pinger is running..");
}

}

FINALLY I SOLVE IT :D
i have no time to explain it but i like to share it with you.

import java.util.Timer;  import java.util.TimerTask;  import sociatag.view.main_frame;  public class devices_pinger implements Runnable {




// PROPERTIES:

private int delay = 1000;   // delay for 1 seconds.
private int period = 6000;  // repeat every 6 seconds.
private boolean stop_timer = false;
// CONSTRACTOR:

public devices_pinger() {
}
// METHODES:

/*
 * because we implements Runnable in this class,
 * this methode run() has to run this class on a seperate thread.
 * - call the start pinger methode
 *
 * @param: event occured on the serial
 */
@Override
public void run() {
    run_pinger();

}

/*
 * this is the start point of this class
 * - create a scheduler to run every x seconds
 * - call the run methode every x seconds to:
 * - send 'P' through the serial
 * - wait x seconds
 * - call the ping analyser to get all the replyed pings and analyse them
 */
private void run_pinger() {

    new Timer().schedule(new TimerTask() {

        @Override
        public void run() {

            // 1) ping the connected device
            serial_connection.serial_write('P');
            System.out.println("> PING (Send P)");//<------TESTING--------------------------------------------
            // pause for 3 seonds to give time for all the devices to response 
            try {
                Thread.sleep(3000); // call the analyser after 3 seconds
            } catch (InterruptedException ex) {
                System.out.println("Error: while pausing the thread");
            } // 2) get the result of the ping to analyze it
            serial_listener.ping_analyser();

            if (stop_timer == true) {
                this.cancel();
            }
        }
    }, delay, period);
}

/*
 * stop the timer before pairing the devices or do other activities
 */
public void stop_pinger() {

    stop_timer = true;
    main_frame.display_notification("Device Pinger is paused.");
}

/*
 * restart the pinger by creating new instance of timer
 */
public void start_pinger() {
    stop_timer = false;
    run_pinger();
    main_frame.display_notification("Device Pinger is running..");
}

}

姜生凉生 2024-12-18 08:10:31

您是否尝试过使用 Runnable 处理程序并将其设置为定期间隔?像这样:

private Handler mUpdateHandler = new Handler();
private Runnable mUpdateRunnable = new Runnable() {
    public void run() {
        mUpdateHandler.postDelayed(this, delay);
    }
};
mUpdateHandler.postDelayed(mUpdateRunnable, delay);

您可以根据需要指定延迟,并且可以在 run() 中执行您需要执行的任何操作。

Have you tried using a Runnable handler and setting it to regular intervals? Something like this:

private Handler mUpdateHandler = new Handler();
private Runnable mUpdateRunnable = new Runnable() {
    public void run() {
        mUpdateHandler.postDelayed(this, delay);
    }
};
mUpdateHandler.postDelayed(mUpdateRunnable, delay);

where you specify the delay as you wish and you can do whatever you need to do in run().

中二柚 2024-12-18 08:10:31

@Alan Moore:嗨,我想我终于做到了!这是能够执行 wifi 扫描并使用广播意图将响应发送回调用活动的代码。

LargeImageScoll.java(活动)

Intent intent;

public static Runnable scanTask;

public BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        //updateUI(intent); 
        //String returnedValue = intent.getStringExtra("data");
        Bundle bundle = intent.getExtras();
        String returnedValue = bundle.getString("data");
        Log.e("Waht", returnedValue);
    }
};

private static Handler handler = new Handler(){

    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        super.handleMessage(msg);
    }

};

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

        intent = new Intent(this, WifiScanning.class);
}
@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 Runnable(){
    @Override
    public void run() {
    // TODO Auto-generated method stub
    startService(new Intent(context, WifiScanning.class));  
    };

    handler.removeCallbacks(scanTask);
    handler.postDelayed(scanTask, refreshRate);
}

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>>{...}

}//end of SampleView

protected void onResume() {
    // TODO Auto-generated method stub
    Log.e("AndroidRuntime", "onResume");
    handler.removeCallbacks(scanTask);
    handler.postDelayed(scanTask, refreshRate);
    registerReceiver(broadcastReceiver, new IntentFilter(WifiScanning.BROADCAST_ACTION));
    super.onResume();
}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    Log.e("AndroidRuntime", "onStop");
    unregisterReceiver(broadcastReceiver);
    stopService(new Intent(this, WifiScanning.class));
    handler.removeCallbacks(scanTask);
    super.onStop();
}
}//end of LargeImageScroll

WifiScanning.java(服务)

public static final String BROADCAST_ACTION = "android.wps.wifiscanning.broadcasttest";
int counter = 0;
Intent intent1;
WifiReceiver receiverWifi = new WifiReceiver();
WifiManager wifi;
StringBuilder sb;
List<ScanResult> wifiList;

public void onCreate() {
    super.onCreate();

    intent1 = new Intent(BROADCAST_ACTION); 
    Log.e(TAG, "Service creating");
    wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    if(counter==0){
    Log.e("AndroidRuntime", "Scan for the "+counter+" time");
    wifi.startScan();
    counter++;
    }
}

class WifiReceiver extends BroadcastReceiver {
    public void onReceive(Context c, Intent intent) {
        sb = new StringBuilder();
        wifiList = wifi.getScanResults();
        for(int i = 0; i < wifiList.size(); i++){
            sb.append("["+ (wifiList.get(i).SSID).toString() + "][");
            sb.append((wifiList.get(i).BSSID).toString() + "][");
            sb.append((String.valueOf(wifiList.get(i).level)) + "]");
            sb.append("\n");
        }
        Log.e("AndroidRuntime", sb.toString());
        if(counter<4){
            Log.e("AndroidRuntime", "Scan for the "+counter+" time");
            wifi.startScan();
            counter++;
        }else{
            intent1 = new Intent(BROADCAST_ACTION);
            String test = sb.toString();
            intent1.putExtra("data", test);
            sendBroadcast(intent1);
        }
    }
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    unregisterReceiver(receiverWifi);
    super.onDestroy();
}

@Alan Moore: Hi, i guess i finally did it! this is the code that was able to perform wifi scan and send back the response back to the calling activity using Broadcast intent.

LargeImageScoll.java (Activity)

Intent intent;

public static Runnable scanTask;

public BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        //updateUI(intent); 
        //String returnedValue = intent.getStringExtra("data");
        Bundle bundle = intent.getExtras();
        String returnedValue = bundle.getString("data");
        Log.e("Waht", returnedValue);
    }
};

private static Handler handler = new Handler(){

    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        super.handleMessage(msg);
    }

};

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

        intent = new Intent(this, WifiScanning.class);
}
@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 Runnable(){
    @Override
    public void run() {
    // TODO Auto-generated method stub
    startService(new Intent(context, WifiScanning.class));  
    };

    handler.removeCallbacks(scanTask);
    handler.postDelayed(scanTask, refreshRate);
}

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>>{...}

}//end of SampleView

protected void onResume() {
    // TODO Auto-generated method stub
    Log.e("AndroidRuntime", "onResume");
    handler.removeCallbacks(scanTask);
    handler.postDelayed(scanTask, refreshRate);
    registerReceiver(broadcastReceiver, new IntentFilter(WifiScanning.BROADCAST_ACTION));
    super.onResume();
}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    Log.e("AndroidRuntime", "onStop");
    unregisterReceiver(broadcastReceiver);
    stopService(new Intent(this, WifiScanning.class));
    handler.removeCallbacks(scanTask);
    super.onStop();
}
}//end of LargeImageScroll

WifiScanning.java (Service)

public static final String BROADCAST_ACTION = "android.wps.wifiscanning.broadcasttest";
int counter = 0;
Intent intent1;
WifiReceiver receiverWifi = new WifiReceiver();
WifiManager wifi;
StringBuilder sb;
List<ScanResult> wifiList;

public void onCreate() {
    super.onCreate();

    intent1 = new Intent(BROADCAST_ACTION); 
    Log.e(TAG, "Service creating");
    wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    if(counter==0){
    Log.e("AndroidRuntime", "Scan for the "+counter+" time");
    wifi.startScan();
    counter++;
    }
}

class WifiReceiver extends BroadcastReceiver {
    public void onReceive(Context c, Intent intent) {
        sb = new StringBuilder();
        wifiList = wifi.getScanResults();
        for(int i = 0; i < wifiList.size(); i++){
            sb.append("["+ (wifiList.get(i).SSID).toString() + "][");
            sb.append((wifiList.get(i).BSSID).toString() + "][");
            sb.append((String.valueOf(wifiList.get(i).level)) + "]");
            sb.append("\n");
        }
        Log.e("AndroidRuntime", sb.toString());
        if(counter<4){
            Log.e("AndroidRuntime", "Scan for the "+counter+" time");
            wifi.startScan();
            counter++;
        }else{
            intent1 = new Intent(BROADCAST_ACTION);
            String test = sb.toString();
            intent1.putExtra("data", test);
            sendBroadcast(intent1);
        }
    }
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    unregisterReceiver(receiverWifi);
    super.onDestroy();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文