我想在手机充电时通知用户,例如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();
}
}
我已经在broadcastreceiver中实现了这个代码,我该如何通知用户, 或者在手机充电时打开应用程序
答案 0 :(得分:0)
在广播接收器中收到回叫时,使用意图启动应用程序。
答案 1 :(得分:0)
如果您的广播接收器被正确调用,则可以使用startactivity()方法打开您的活动或应用程序。
这样的事情:
startActivity(new Intent(getApplicationContext(), youractivityname.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
答案 2 :(得分:0)
如果您想要打开活动,您只需为活动生成intent
。
Intent intent = new Intent(Context, YourActivityClass.class);
startActivity(intent);
如果您想要通知用户某些通知,您可以 如果您想了解有关意图以及如何开始活动的更多信息,您应该创建一个通知:
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());
查看有关intents或有关通知[2]的Android文档。
答案 3 :(得分:0)
检查电池的状态(充电与否)..使用此代码..
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;
}
然后,如果此函数返回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());
答案 4 :(得分:0)
首先准备广播接收器以侦听对ACTION_POWER_CONNECTED的更改,然后标记屏幕以保持开启状态。 然后在onReceive of Broadcast Receiver-
public void onReceive { 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 ||插入== BatteryManager.BATTERY_PLUGGED_USB; }}
之后只需通知用户
NotificationCompat.Builder builder = 新NotificationCompat.Builder(上下文) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("电话状态") .setContentText("充电&#34);
int notificationId = 12345;
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(notificationId,builder.build());