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-IllegalStat
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".
@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();
}
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().
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..");
}
}