Posts

Showing posts from December, 2013

Android: Getting to Wifi scan results without consuming additional battery

In previous post , I have shown how we can start the scanning of wifi and receive the results using BroadcastReceiver. The problem with this approach is it consumes battery each time scanning is started. To avoid this there is another way: Listen to scan results initiated by other applications or by OS. You need to add following in manifest. <receiver android:name="com.example.receivers.WifiScanCompleteReceiver" > <intent-filter> <action android:name="android.net.wifi.SCAN_RESULTS" /> </intent-filter> </receiver> Code for WifiScanCompleteReceiver.java is as follows: package com.example.receivers; //imports public class WifiScanCompleteReceiver extends BroadcastReceiver { public WifiScanCompleteReceiver() {} @ Override public void onReceive(Context context, Intent intent) { Log.d(HelperUtils.getInstance().getApplicationName(), "Wifi scan result received."); Vector < String > WifiNames = new Vector ...

Formatting and showing code in Blogger

A good link for formatting and showing code in Blogger. http://stackoverflow.com/questions/10335463/how-to-setup-syntax-highlighter-on-blogger/10848816#10848816

Android: Adding and removing an alarm event (to be repeated each day)

Following code shows how to add an alarm for a given time along with setting a number as extra to identify which alarm was triggered. ProfileSettings.java is a class containing following fields with getter setter. Class ProfileSettings {     private long _time;     private int _type;     private int _id; } Code to add the alarm is as follows: public void addAlarmEvent(ProfileSettings setting) { Context context = Main.getInstance().getContext(); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, AlarmBroadcastReceiver.class); intent.putExtra(AlarmBroadcastReceiver.PROFILE_CODE, setting.getProfileCode()); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, setting.getEventId(), intent, PendingIntent.FLAG_UPDATE_CURRENT); Long time = setting.getTime(); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, AlarmManager.INTERV...