Hi,today we learn about power connection and disconnection in android.For this requirement we have to use BroadcastReceiver and intent-filter.
BroadcastReceiver :This is one of android component. it handle communication between android os and applications.
First ,we have to register BroadcastReceiver in AndroidManifest.xml
<receiver android:name=".PlugInConnector">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ypp.battery"
android:versionCode="4"
android:versionName="1.3" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
<activity
android:name="com.ypp.battery.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.dc.battery.PlugInConnector">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
</application>
</manifest>
PlugInConnector.java
copy and paste the below code in your created java file
public class PlugInConnector extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(Intent.ACTION_POWER_CONNECTED))
{
// Do something when power connected
Toast.makeText(context, "connected", 3000).show();
}
else if(action.equals(Intent.ACTION_POWER_DISCONNECTED))
{
// Do something when power disconnected
Toast.makeText(context, "Disconnected", 3000).show();
}
}
}
Finally, Run your application
then connect your device to power
we will get result like below
connected : this will be display when your device connected to power
Disconnected: this will be display when your device disconnected from power
BroadcastReceiver :This is one of android component. it handle communication between android os and applications.
First ,we have to register BroadcastReceiver in AndroidManifest.xml
<receiver android:name=".PlugInConnector">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ypp.battery"
android:versionCode="4"
android:versionName="1.3" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
<activity
android:name="com.ypp.battery.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.dc.battery.PlugInConnector">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
</application>
</manifest>
copy and paste the below code in your created java file
public class PlugInConnector extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(Intent.ACTION_POWER_CONNECTED))
{
// Do something when power connected
Toast.makeText(context, "connected", 3000).show();
}
else if(action.equals(Intent.ACTION_POWER_DISCONNECTED))
{
// Do something when power disconnected
Toast.makeText(context, "Disconnected", 3000).show();
}
}
}
Finally, Run your application
then connect your device to power
we will get result like below
connected : this will be display when your device connected to power
Disconnected: this will be display when your device disconnected from power