Search code examples
androidandroid-wifiandroid-alarms

How to check AlarmManager for repeating only 5 times


I ve been stuck with the time for a while, so I decided to use AlarmManager but I m facing the problem, Actually I would like to

  1. scan Wifi and insert into SQlite every 3 seconds for 5 times, when finishing at fifth, actually select avg(of wifi signal) from sqlite).

  2. After showing avg value, the system will continue doing the scanning in 1. again, the system will stop until user clicks stop button.

    But the thing is I don't know how use AlarmManager do such a thing like that.

Please anyone recommends me. Thank you.

(sorry for unorganized code) Here is my rough code:

long repeatTime = 1000*3;
String fullinfo;
final public static String ONE_TIME = "onetime";
AlarmManager am ;
PendingIntent pi;
public void onReceive(Context context, Intent intent) {
    PowerManager pm = (PowerManager) context
            .getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(
            PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
    wl.acquire();


    WifiManager wifiMan = (WifiManager) context
            .getSystemService(Context.WIFI_SERVICE);
    wifiMan.startScan();
    List<ScanResult> resultList = wifiMan.getScanResults();
    int foundCount = resultList.size();
    Toast.makeText(context, "Scan done, " + foundCount + " found",
            Toast.LENGTH_SHORT).show();
    ListIterator<ScanResult> results = resultList.listIterator();
    fullinfo = "Scan Results : \n";
    String[][] wifi_info = new String[foundCount - 1][3];
    for (int i = 0; i <= foundCount - 1; i++) {
        while (results.hasNext()) {
            ScanResult info = results.next();

            wifi_info[i][0] = "\nName: " + info.SSID + "\n";
            wifi_info[i][1] = ";Mac: " + info.BSSID + "\n";
            wifi_info[i][2] = ";Signal: " + info.level + "\n";

            fullinfo = fullinfo + wifi_info[0][0] + wifi_info[0][1]
                    + wifi_info[0][2];

        }// end while
    }// end for


    Bundle extras = intent.getExtras();
    StringBuilder msgStr = new StringBuilder();

    if (extras != null && extras.getBoolean(ONE_TIME, Boolean.FALSE)) {
        msgStr.append("One time Timer : ");
    }
    Format formatter = new SimpleDateFormat("hh:mm:ss a");
    msgStr.append(formatter.format(new Date()));

    //insertIntoDatabase();
    Toast.makeText(context, "" + fullinfo + "\n" + msgStr + "_"+currentTime,
            Toast.LENGTH_SHORT).show();
    wl.release();

}

public void SetAlarm(Context context) {
         am = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        Intent intent = new  Intent(context,AlarmManagerBroadcastReceiver.class);
        intent.putExtra(ONE_TIME, Boolean.FALSE);
         pi = PendingIntent.getBroadcast(context, 0, intent, 0);
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
                repeatTime, pi);
}

Solution

  • I think instead of using alarm manager you can use a countDownTimer as follows

    new CountDownTimer(15000, 3000) {
    
                @Override
                public void onTick(long arg0) {
                    // Your code here
    
                }
    
                @Override
                public void onFinish() {
                    // your code here
    
                }
            }.start();
    

    This count down timer will work for 15 seconds (5*3sec) with an interval of 3 seconds. You can use onTick() to write your code to execute in each 3 seconds and onFinish() to write the code for finishing the timer after 5 intervals.