我的应用程序有一个向WindowManager添加浮动按钮的服务.
我想从WindowManager中删除我的浮动按钮当用户按下电源键并关闭屏幕时.所以当用户在我的浮动按钮上打开屏幕时不会隐藏(掩码)android模式屏幕锁定.
public class Receiver extends broadcastReceiver { @Override public void onReceive(Context context,Intent intent) { if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { try{ // Remove Floating Button from Window Manager MyWindowManager.removeView(floating_btn); // Stop Service stopSelf(); } catch (Exception e) { //Log Error } } } }
解决方法
通常,您会在清单中声明
receiver.像这样的东西
<receiver android:name="com.whatever.client.Receiver" <intent-filter> <action android:name="android.intent.action.SCREEN_OFF" /> </intent-filter> </receiver>
出于某种原因(不确定原因),您似乎无法为SCREEN_OFF或SCREEN_ON执行此操作.所以你必须以编程方式注册它.
作为测试,我制作了一个简单的应用程序.
public class App extends Application { @Override public void onCreate() { super.onCreate(); broadcastReceiver receiver = new broadcastReceiver() { @Override public void onReceive(Context context,Intent intent) { if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { startService(new Intent(context,MyService.class)); } } }; IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_SCREEN_OFF); registerReceiver(receiver,filter); } }
提供简单的服务.
public class MyService extends IntentService { public MyService() { super("MyService"); } @Override protected void onHandleIntent(Intent intent) { Log.e("MyService","Screen was turned off!"); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。