简体   繁体   English

我想在手机充电时通知用户,例如DUbattery saver应用在手机充电时通过屏幕覆盖通知

[英]i want to notify the user when phone is charging,like DUbattery saver app notifies by screen overlay when phone is charging

i want to notify the user when phone is charging,like DUbattery saver app notifies by screen overlay when phone is charging 我想在手机充电时通知用户,例如DUbattery saver应用在手机充电时通过屏幕覆盖通知

 public void onReceive(Context context, Intent intent) {
    try {

        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = context.registerReceiver(null, ifilter);

        int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                status == BatteryManager.BATTERY_STATUS_FULL;

        if (isCharging == true) {
            Toast.makeText(context, "PLEASE UNPLUG", Toast.LENGTH_LONG).show();
        }
    }

i have implemented this code in broadcastreceiver,how can i notify the user , or by opening the the app when phone is charging 我已经在广播接收器中实现了此代码,如何通知用户,或者在手机充电时打开应用程序

在广播接收器中收到回调时,请使用意图启动您的应用程序。

If your broadcast receiver is getting called properly than you can open your activity or application using startactivity() method. 如果正确调用了广播接收器,则可以使用startactivity()方法打开您的活动或应用程序。

Something like this : 像这样的东西:

startActivity(new Intent(getApplicationContext(), youractivityname.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));

If you want to open the activity, all you need to do is generate a intent for your activity. 如果要打开活动,您要做的就是为活动生成一个intent

Intent intent = new Intent(Context, YourActivityClass.class);
startActivity(intent);

If otherwise you want to notify the user with some notification, you can If you want further info about intents and how start your activity, you should create a notification: 否则,如果您想通过一些通知来通知用户,则可以。如果您想要有关意图以及如何开始活动的更多信息,则应该创建一个通知:

NotificationCompat.Builder mBuilder =
    new NotificationCompat.Builder(context)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("My notification")
    .setContentText("Hello World!");
// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = 
        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());

Take a look to Android documentation about intents or about notification[2]. 查看有关意图或通知的Android文档[2]。

To check the state of the battery(charging or not)..use this code.. 要检查电池的状态(是否正在充电)。使用此代码。

public static boolean isConnected(Context context) {
    Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
}

Then if this function returns true..show the notification by this code.. 然后,如果此函数返回true,则显示此代码的通知。

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentTitle("Charging State")
    .setContentText("Your phone is charging");
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());

First prepare broadcast receiver to listen for changes to ACTION_POWER_CONNECTED, and in turn flag the screen to stay on. 首先,准备广播接收器以监听对ACTION_POWER_CONNECTED的更改,然后标记屏幕以保持打开状态。 Then in onReceive of Broadcast Receiver- 然后在onReceive of Broadcast Receiver中

public void onReceive { public static boolean isConnected(Context context) { Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 公共无效onReceive {公共静态布尔isConnected(Context上下文){Intent intent = context.registerReceiver(null,new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); 插入的int = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,-1); return plugged == BatteryManager.BATTERY_PLUGGED_AC || 返回已插入== BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB; 已插入== BatteryManager.BATTERY_PLUGGED_USB; } } }}

After that just simply notify the user with notification 之后,只需通知用户通知

NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("Phone Status") .setContentText("Charging"); NotificationCompat.Builder builder =新的NotificationCompat.Builder(context).setSmallIcon(R.drawable.notification_icon).setContentTitle(“电话状态”).setContentText(“收费”);

int notificationId = 12345; int notificationId = 12345;

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); NotificationManager notificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

notificationManager.notify(notificationId, builder.build()); notificationManager.notify(notificationId,builder.build());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM