android 屏幕解锁时如何取消 LED 通知?
how to cancel led notification , when screen is unlocked in android?
我有一个应用程序,当飞行模式打开时,led 灯通知开始。我想停止或取消这个 LED 灯,当我的屏幕是 unlocked.how 我可以处理这个吗?
我尝试了这个但没有成功:
....
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = powerManager.isScreenOn();
if (isScreenOn) {
stop();
}
}
public void stop(){
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(NOTIFICATION_ID);
}
}
如果我想取消这个led灯,当飞行模式关闭时,我该如何检查这个事件?
谢谢...
你的代码在哪里?
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = powerManager.isScreenOn();
if (isScreenOn) {
stop();
}
我相信这里没有任何问题,但如果你在 onCreate()
上声明它,它将不起作用。
您需要注册一个接收器。
registerReceiver(this.screenOffListener, new IntentFilter("android.intent.action.SCREEN_OFF"));
并在正确的地方声明你的代码
public class ScreenOffListener extends BroadcastReceiver
{
public ScreenOffListener() {}
public void onReceive(Context paramContext, Intent paramIntent)
{
// place your code
}
}
首先,处理 (ACTION_USER_PRESENT) http://developer.android.com/reference/android/content/Intent.html#ACTION_USER_PRESENT -- 这是设备解锁的时间。然后取消。
<receiver android:name=".Receive">
<intent-filter android:enabled="true" android:exported="false">
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
public class Receive extends BroadcastReceiver {
if (intent.getAction() != null) {
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
if (Context.NOTIFICATION_SERVICE != null) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) getApplicationContext().getSystemService(ns);
nMgr.cancel(0);
}
}
}
}
我没有测试过这段代码,所以你可能需要修改它。但希望这个概念是正确的。
我有一个应用程序,当飞行模式打开时,led 灯通知开始。我想停止或取消这个 LED 灯,当我的屏幕是 unlocked.how 我可以处理这个吗?
我尝试了这个但没有成功:
....
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = powerManager.isScreenOn();
if (isScreenOn) {
stop();
}
}
public void stop(){
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(NOTIFICATION_ID);
}
}
如果我想取消这个led灯,当飞行模式关闭时,我该如何检查这个事件?
谢谢...
你的代码在哪里?
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = powerManager.isScreenOn();
if (isScreenOn) {
stop();
}
我相信这里没有任何问题,但如果你在 onCreate()
上声明它,它将不起作用。
您需要注册一个接收器。
registerReceiver(this.screenOffListener, new IntentFilter("android.intent.action.SCREEN_OFF"));
并在正确的地方声明你的代码
public class ScreenOffListener extends BroadcastReceiver
{
public ScreenOffListener() {}
public void onReceive(Context paramContext, Intent paramIntent)
{
// place your code
}
}
首先,处理 (ACTION_USER_PRESENT) http://developer.android.com/reference/android/content/Intent.html#ACTION_USER_PRESENT -- 这是设备解锁的时间。然后取消。
<receiver android:name=".Receive">
<intent-filter android:enabled="true" android:exported="false">
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
public class Receive extends BroadcastReceiver {
if (intent.getAction() != null) {
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
if (Context.NOTIFICATION_SERVICE != null) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) getApplicationContext().getSystemService(ns);
nMgr.cancel(0);
}
}
}
}
我没有测试过这段代码,所以你可能需要修改它。但希望这个概念是正确的。