How to Detect Power Connected State

How to detect power connected state?

Set up a BroadcastReceiver for ACTION_BATTERY_CHANGED. An Intent extra will tell you what the charging state is -- see BatteryManager for details.

detect power state change

The SystemEvents.PowerModeChanged event should do what you want

You might also want to check out the SystemInformation.PowerStatus property.

How to listen for power connected Android 8

The question you have referred already has the answer.

Use ACTION_BOOT_COMPLETED to start a foreground service after boot. This service should register ACTION_POWER_CONNECTED broadcast. You can also start this service in a separate process. As soon as the power is connected/disconnected, you will receive the broadcast in service class where you can run your required method.

public class MyService extends Service {
private String TAG = this.getClass().getSimpleName();

@Override
public void onCreate() {
Log.d(TAG, "Inside onCreate() API");
if (Build.VERSION.SDK_INT >= 26) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.ic_launcher);
mBuilder.setContentTitle("Notification Alert, Click Me!");
mBuilder.setContentText("Hi, This is Android Notification Detail!");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// notificationID allows you to update the notification later on.
mNotificationManager.notify(100, mBuilder.build());
startForeground(100, mBuilder.mNotification);

IntentFilter filter1=new IntentFilter();
filter1.addAction(Intent.ACTION_POWER_CONNECTED);
registerReceiver(myBroadcastReceiver,filter1);
}
}

@Override
public int onStartCommand(Intent resultIntent, int resultCode, int startId) {
Log.d(TAG, "inside onStartCommand() API");
return startId;
}

@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "inside onDestroy() API");
}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

BroadcastReceiver myBroadcastReceiver =new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// call your method
}
};
}

You can also register a JobScheduler with setRequiresCharging true. this will start the call JobScheduler's job when the power state is changed.

ComponentName serviceComponent = new ComponentName(context, TestJobService.class);
JobInfo.Builder builder = new JobInfo.Builder(0, serviceComponent);
builder.setMinimumLatency(1 * 1000); // wait at least
builder.setOverrideDeadline(3 * 1000); // maximum delay
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED);
builder.setRequiresDeviceIdle(true);
builder.setRequiresCharging(true);
JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
jobScheduler.schedule(builder.build());

How do I detect if device is charging from background

The best way to accomplish this is by having a service run in the background so that you can receive the broadcast from android without the user using the app.

Start by registering a service in your manifest file.

<Service android:name=".serviceName"/>

Now create your class with the same name as registered in the manifest above, this class must extend Service.

    public class BackgroundService extends Service {
private static final int NOTIF_ID = 1;
private static final String NOTIF_CHANNEL_ID = "AppNameBackgroundService";

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId){

//Add broadcast receiver for plugging in and out here
ChargeDetection chargeDetector = new ChargeDetection(); //This is the name of the class at the bottom of this code.

IntentFilter filter = new IntentFilter();
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
filter.addAction(Intent.ACTION_POWER_CONNECTED);
filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
this.registerReceiver(chargeDetector, filter);

startForeground();

return super.onStartCommand(intent, flags, startId);
}

private void startForeground() {
createNotificationChannel();
Intent notificationIntent = new Intent(this, MainActivity.class);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);

startForeground(NOTIF_ID, new NotificationCompat.Builder(this,
NOTIF_CHANNEL_ID) // don't forget create a notification channel first
.setOngoing(true)
.setSmallIcon(R.mipmap.final_icon)
.setContentTitle(getString(R.string.app_name))
.setContentText("Background service is running")
.setContentIntent(pendingIntent)
.build());
}

private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
NOTIF_CHANNEL_ID,
"Foreground Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}
}

class ChargeDetection extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Now check if user is charging here. This will only run when device is either plugged in or unplugged.
}
}
}

How to detect if the phone is connected to power in flutter

You can use the battery package

How it looks like:

// Import package
import 'package:battery/battery.dart';

// Instantiate it
var battery = Battery();

// Access current battery level
print(await battery.batteryLevel);

var _batteryState;
_battery.onBatteryStateChanged.listen((BatteryState state) {
_batteryState = state;
print(_batteryState);
});

How do I tell if a windows mobile device is connected to external power using VB.Net?

If you're using WIndowsMobile 5.0 and later only, the State and Notification Broker is where to look, specifically at the Status namespace.

For broader support, you can detect the state transition (to or from AC power) calling CeRunAppAtEvent (this can set a named event rather than just running an app) with the NOTIFICATION_EVENT_AC_APPLIED or NOTIFICATION_EVENT_AC_REMOVED event codes. This is what the DeviceManagement class in the Smart Device Framework does.

You can detect current state (instead of transitions) by calling GetSystemPowerStatusEx2.



Related Topics



Leave a reply



Submit