使用服务注册特殊广播接收者
2018-03-09 08:06
309 查看
使用服务注册特殊广播,比如注册手机锁屏或解锁的广播:
首先这是定义的广播接收者:
动态注册广播的Service:
MainActivity在加载的时候就开启服务:
在配置文件中配置一下service就OK
好了,一切就绪,效果如下:
首先这是定义的广播接收者:
package useservice.xpu.nevergiveup.serviceresgitreceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //获取当前事件类型 String action = intent.getAction(); // if("android.intent.action.SCREEN_OFF".equals(action)){ //屏幕锁屏 System.out.println("屏幕锁屏"); }else if("android.intent.action.SCREEN_ON".equals(action)){ //屏幕解锁 System.out.println("屏幕解锁"); } } }
动态注册广播的Service:
package useservice.xpu.nevergiveup.serviceresgitreceiver; import android.app.Service; import android.content.Intent; import android.content.IntentFilter; import android.os.IBinder; public class ScreenService extends Service { private MyReceiver myReceiver; public ScreenService() { } @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { //获取MyReceiver实例 myReceiver = new MyReceiver(); //添加Action IntentFilter filter = new IntentFilter(); filter.addAction("android.intent.action.SCREEN_OFF"); filter.addAction("android.intent.action.SCREEN_ON"); //动态注册广播 registerReceiver(myReceiver,filter); super.onCreate(); } @Override public void onDestroy() { //当服务销毁的时候取消注册广播 unregisterReceiver(myReceiver); super.onDestroy(); } }
MainActivity在加载的时候就开启服务:
package useservice.xpu.nevergiveup.serviceresgitreceiver; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startService(new Intent(getApplicationContext(),ScreenService.class)); } }
在配置文件中配置一下service就OK
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="useservice.xpu.nevergiveup.serviceresgitreceiver"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".ScreenService"/> </application> </manifest>
好了,一切就绪,效果如下:
相关文章推荐
- 使用服务注册特殊广播接收者
- 使用服务注册特殊的广播接收者
- android服务_使用服务注册特殊的广播接收者
- Android-使用服务注册广播接收者
- 使用服务注册广播接收者
- Android 使用服务注册广播接收者
- Android初级教程使用服务注册广播接收者监听手机解锁屏变化
- Android初级教程使用服务注册广播接收者监听手机解锁屏变化
- Android(java)学习笔记94:关于广播接收者的注册和使用心得
- 特殊的广播接收者(界面注册)
- 特殊的广播接收者(界面注册)
- android,利用广播接收者调用服务的方法,代码注册广播接收者
- 特殊的广播接收者(界面注册)
- BroadCastReceiver(广播接收者的静态注册)简单使用例子
- 代码注册广播接收者,利用广播调用服务中的方法
- 使用服务动态注册,销毁广播
- Androidx学习笔记(67)--- 使用代码注册广播接收者(实现屏幕锁屏和解锁)
- 【黑马Android】(08)activity生命周期/横竖屏切换的生命周期/任务栈的概念/广播接收者ip拨号器/进程的优先级&为什么使用服务
- 47.android服务service-代码注册广播接收者
- [android] 代码注册广播接收者&利用广播调用服务的方法