Monitoring battery use. New app, BatteryMonitor Add permission: BATTERY_STATS The battery is monitored by receiving broadcasts of battery state information.

Slides:



Advertisements
Similar presentations
Android Application Development Tutorial. Topics Lecture 6 Overview Programming Tutorial 3: Sending/Receiving SMS Messages.
Advertisements

1 Android Introduction Hello Threads. 2 Goal Create an application that uses a background thread as a UDP server to receive messages from the UDP client.
1 Working with the Bluetooth radio Nilanjan Banerjee Mobile Systems Programming University of Arkansas Fayetteville, AR
Services. Application component No user interface Two main uses Performing background processing Supporting remote method execution.
WiFi in Android.
Chapter 6 Jam! Implementing Audio in Android Apps.
Chapter 6: Jam! Implementing Audio in Android Apps.
All About Android Introduction to Android 1. Creating a New App “These aren’t the droids we’re looking for.” Obi-wan Kenobi 1. Bring up Eclipse. 2. Click.
BroadcastReceiver.  Base class for components that receive and react to events  Events are represented as Intent objects  Intents received as parameter.
Hello world Follow steps under the sections “Create an AVD” and “Create a New Android Project” at
1 Working with the Android Services Nilanjan Banerjee Mobile Systems Programming University of Arkansas Fayetteville, AR
Cosc 5/4730 Android SMS. A note first Depending on the API level, an import changes because of a deprecated API 3 uses – import android.telephony.gsm.SmsManager;
System broadcasts and services. System broadcast events. EventDescription Intent.ACTION_BOOT_COMPLETEDBoot completed. Requires the android.permission.RECE.
SMS Read received SMS Read sent SMS Send SMS New app, SMSFun Button – Text: Send SMS – Id: sendSMSButton Permissions – send_sms – read_sms – receive_sms.
SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.
Cosc 4730 Android TabActivity and ListView. TabActivity A TabActivity allows for multiple “tabs”. – Each Tab is it’s own activity and the “root” activity.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
Android Sensors & Async Callbacks Jules White Bradley Dept. of Electrical and Computer Engineering Virginia Tech
Monitoring battery power. overview Battery usage is critical on mobile apps Measuring energy usage is a bit tricky since many processes Android provides.
Developing Push Notifications (C2DM) for Android Vijai Co-Founder Adhish Technologies, Sweet’N’Spicy apps.
Mobile Computing Lecture#08 IntentFilters & BroadcastReceivers.
Hello world Follow steps under the sections “Create an AVD” and “Create a New Android Project” at
Broadcast intents.
P2P communication Using the GTalk Service API. Introduction Peer-to-Peer communication highly used in mobile devices. Very efficient to when certain factors.
Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS.
Android Services Mobile Application Development Selected Topics – CPIT Oct-15.
Using Intents to Broadcast Events Intents Can be used to broadcast messages anonymously Between components via the sendBroadcast method As a result Broadcast.
Cosc 5/4730 Broadcast Receiver. Broadcast receiver A broadcast receiver (short receiver) – is an Android component which allows you to register for system.
Android ICC Part II Inter-component communication.
COMP 365 Android Development.  Perform operations in the background  Services do not have a user interface (UI)  Can run without appearing on screen.
Mobile Programming Lecture 6
로봇 모니터링 2/2 UNIT 21 로봇 SW 콘텐츠 교육원 조용수. 학습 목표 Broadcasting Service 2.
16 Services and Broadcast Receivers CSNB544 Mobile Application Development Thanks to Utexas Austin.
Android Accessing GPS Ken Nguyen Clayton State University 2012.
Android Programming-Activity Lecture 4. Activity Inside java folder Public class MainActivity extends ActionBarActivity(Ctrl + Click will give you the.
Threads and Services. Background Processes One of the key differences between Android and iPhone is the ability to run things in the background on Android.
Android - Broadcast Receivers
Tracking Tasks and Processes. GET_TASK Make a new app, WatchProcesses – Add permission GET_TASK This permission allows the app to collect lots of information.
Import import android.graphics.Bitmap; import android.widget.ImageView;
Services A Service is an application component that can perform long-running operations in the background and does not provide a user interface. An application.
Activity Android Club Agenda Hello Android application Application components Activity StartActivity.
Android Boot Camp Demo Application – Part 1. Development Environment Set Up Download and install Java Development Kit (JDK) Download and unzip Android.
Mobile Programming Midterm Review
Android Application Lifecycle and Menus
A: A: double “4” A: “34” 4.
Services Background operating component without a visual interface Running in the background indefinitely Differently from Activity, Service in Android.
Lecture 2: Android Concepts
Speech Service & client(Activity) 오지영.
Intents and Broadcast Receivers Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution.
David Sutton SMS TELEPHONY IN ANDROID. OUTLINE  This week’s exercise, an SMS Pub Quiz  Simulating telephony on an emulator  Broadcast Intents and broadcast.
Services. What is a Service? A Service is not a separate process. A Service is not a thread. A Service itself is actually very simple, providing two main.
Messaging and Networking. Objectives Send SMS messages using the Messaging app Send SMS messages programmatically from within your app Receive and consume.
Developing Android Services. Objectives Creating a service that runs in background Performing long-running tasks in a separate thread Performing repeated.
Introduction to android
Intents and Broadcast Receivers
Lecture 2: Android Concepts
Activity and Fragment.
Broadcast receivers.
Reactive Android Development
Developing Android Services
Android Notifications (Part 2) Plus Alarms and BroadcastReceivers
null, true, and false are also reserved.
CIS 470 Mobile App Development
Android Developer Fundamentals V2 Lesson 5
Service Services.
CIS 470 Mobile App Development
Notifying from the Background
CIS 470 Mobile App Development
Mobile Programming Broadcast Receivers.
CA16R405 - Mobile Application Development (Theory)
Presentation transcript:

Monitoring battery use

New app, BatteryMonitor Add permission: BATTERY_STATS The battery is monitored by receiving broadcasts of battery state information Register to receive broadcasts In onCreate add – IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); – registerReceiver(batteryLevelReceiver, batteryLevelFilter); Add broadcast receiver as activity member variable. – BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() { – public void onReceive(Context context, Intent intent) { – context.unregisterReceiver(this); – int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); – int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); – int level = -1; – if (rawlevel >= 0 && scale > 0) { – level = (rawlevel * 100) / scale; – } – Log.e("DEBUG","Battery Level Remaining: " + level + "%"); – } – }; run

Show more battery things Add member variable – TextView textView; – String batteryStr = new String(); Add TextView to layout – Set Misc.LayoutHeight = fill_parent – Set Misc.LayoutWidth = fill_parent In onCreate, add – textView = (TextView)findViewById(R.id.TextView01); – textView.setMovementMethod(new ScrollingMovementMethod()); In batteryLevelReceiver.onReceive, add to beginning – batteryStr += " \n"; – Calendar calendar = new GregorianCalendar(); – batteryStr += "time: "+calendar.get(Calendar.HOUR)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND)+" \n"; Change Log.e("DEBUG","Battery Level Remaining: " + level + "%");, to – batteryStr += "Battery Level Remaining: " + level + "%\n"; At the end of batteryLevelReceiver.onReceive, add – textView.setText(batteryStr); Run – Plug and unplug until textview is full and check that view is scrollable

Battery plugged state In batteryLevelReceiver.onReciever, before textView.setText…, add – int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); – switch (plugged) { – case 0: – batteryStr += "unplugged\n"; – break; – case BatteryManager.BATTERY_PLUGGED_AC: – batteryStr += "Plugged into AC\n"; – break; – case BatteryManager.BATTERY_PLUGGED_USB: – batteryStr += "Plugged into USB\n"; – break; – default: batteryStr += "unknown plugged state\n"; – }

Battery status int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); switch (status) { case BatteryManager.BATTERY_STATUS_CHARGING: batteryStr += "battery is charging\n"; break; case BatteryManager.BATTERY_STATUS_DISCHARGING: batteryStr += "battery is discharging\n"; break; case BatteryManager.BATTERY_STATUS_FULL: batteryStr += "battery is full\n"; break; case BatteryManager.BATTERY_STATUS_NOT_CHARGING: batteryStr += "battery is not charging\n"; break; case BatteryManager.BATTERY_STATUS_UNKNOWN : batteryStr += "battery status is unknown\n"; break; default: batteryStr += "battery status should not happen\n"; break; }

Battery type, temperature, voltage, String tech = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY); batteryStr += "technology: " + tech +"\n"; int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1); batteryStr += "temp: " + temp+"\n"; int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1); batteryStr += "voltage: " + voltage+"\n";

Battery health int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH,-1); switch (health) { /*case BatteryManager.BATTERY_HEALTH_COLD: batteryStr += "health = cold\n"; break; //in documentation, but not supported */ case BatteryManager.BATTERY_HEALTH_DEAD: batteryStr += "health = dead\n"; break; case BatteryManager.BATTERY_HEALTH_GOOD: batteryStr += "health = good\n"; break; case BatteryManager.BATTERY_HEALTH_OVERHEAT: batteryStr += "health = overheat\n"; break; case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE: batteryStr += "health = over voltage\n"; break; case BatteryManager.BATTERY_HEALTH_UNKNOWN: batteryStr += "health = unknown\n"; break; case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE: batteryStr += "health = unspecified failure\n"; break; default: batteryStr += "health state: should not happen\n"; break; }

More fun with battery level Let’s estimate battery charge/discharge rate Add member variables – long lastTime = 0; – double lastLevel; After level is computed, add – double rate = 0; – double terminationTime=0; – if (lastTime!=0) { rate = (level - lastLevel)/(calendar.getTimeInMillis()-lastTime)*1000.0; if (rate>0) { – terminationTime = (100-level)/rate/3600; – batteryStr += "charged in "+terminationTime+" hours\n"; } if (rate<0) { – terminationTime = -level/rate/3600; – batteryStr += "discharged in "+terminationTime+" hours\n"; } – } – lastTime = calendar.getTimeInMillis(); – lastLevel = level;

Battery Monitor Service Make new class – BatteryMonitorService – Derived from android.app.Service To manifest, add service, i.e, – In Application tab Application nodes, add Select radio button “Create a new element at the top level, in application” Select “service” Select ok – With the new service highlighted, In name (on the right), add – BatteryMonitorService Or, select “browse” and (wait) and select BatteryMonitorService

In public void onCreate() { – super.onCreate(); – Log.d("BatteryMonitorService","Created"); – IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); registerReceiver(batteryLevelReceiver, batteryLevelFilter); } private final Binder binder = new LocalBinder(); public class LocalBinder extends Binder { – BatteryMonitorService getService() { Log.d("BatteryMonitorService","localbinder"); return BatteryMonitorService.this; – } } Change onBind, public IBinder onBind(Intent intent) { – // TODO Auto-generated method stub – return binder; public void onDestroy() { – super.onDestroy(); – Log.d("BatteryMonitorService","Destroyed"); – unregisterReceiver(batteryLevelReceiver); }

In BatteryMonitorService Move broadcast receiver from activity to service Move member variables – String batteryStr = new String(); – long lastTime = 0; – double lastLevel; BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { ……

In BatteryMonitor Start service. In OnCreate, add – startService(new Intent(this, BatteryMonitorService.class)); – bindService(new Intent(this, BatteryMonitorService.class), connectionToMyService, BIND_AUTO_CREATE); Add member variable – boolean started = false; After onCreate, add – private BatteryMonitorService batteryMonitorService = null; – private ServiceConnection connectionToMyService = new ServiceConnection() public void onServiceConnected(ComponentName className, IBinder rawBinder) { – batteryMonitorService = ((BatteryMonitorService.LocalBinder)rawBinder).getService(); – started = true; public void onServiceDisconnected(ComponentName name) { – batteryMonitorService = null; – started = false; } – };

In BatteryMonitor We need to get the batteryStr from the service and post it on the textview. – We can use a intent to send the message to get the string – IntentFilter intentFilter = new IntentFilter(); – intentFilter.addAction("edu.udel.eleg454.NewBatteryMonitorStringIsReady"); – registerReceiver(new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { – Log.d("DEBUG","Received intent "+intent); – textView.setText(batteryMonitorService.batteryStr); } – },intentFilter); Let’s get the string when the UI becomes active after being out of public void onResume() { – super.onResume(); – if (started) { textView.setText(batteryMonitorService.batteryStr); – } } // note: batteryMonitorService is only valid after the connection with the service has been established. See connectionToMyService

In BatteryMonitorService At the end of BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() { …. – Remove – textView.setText(batteryStr); Add – Intent broadcastIntent = new Intent(); – broadcastIntent.setAction("edu.udel.eleg454.NewBatteryMonitorStringIsReady"); – sendBroadcast(broadcastIntent); Run and play with gps on, wifi, etc. and see impact on battery usage