镜像镜像08-16 00:54

Android framework自定义待机广播(可静态注册)

Android framework自定义待机广播

由于android.intent.action.SCREEN_ON和android.intent.action.SCREEN_OFF只能在代码中动态注册
android系统需要监听待机唤醒的动作时,常常会时机把握不准,这时候就需要静态注册广播,常驻系统:
  • 修改\frameworks\base\services\java\com\android\server\power\Notifier.java文件

    添加广播
    mScreenOnIntent = new Intent(Intent.ACTION_SCREEN_ON);
    mScreenOnIntent_new = new Intent("android.intent.action.SCREEN_ON_NEW");//此处添加需要的广播
    mScreenOnIntent.addFlags(
            Intent.FLAG_RECEIVER_REGISTERED_ONLY | Intent.FLAG_RECEIVER_FOREGROUND);
    mScreenOffIntent = new Intent(Intent.ACTION_SCREEN_OFF);
    mScreenOffIntent_new= new Intent("android.intent.action.SCREEN_OFF_NEW");//此处添加需要的广播
    mScreenOffIntent.addFlags(
            Intent.FLAG_RECEIVER_REGISTERED_ONLY | Intent.FLAG_RECEIVER_FOREGROUND);
    mSuspendStartIntent = new Intent(SMART_SUSPEND_ENTER);
    
    
    发送唤醒广播
    if (ActivityManagerNative.isSystemReady()) {
        mContext.sendOrderedBroadcastAsUser(mScreenOnIntent, UserHandle.ALL, null,
                mWakeUpBroadcastDone, mHandler, 0, null, null);
        mContext.sendBroadcastAsUser(mScreenOnIntent_new, UserHandle.CURRENT_OR_SELF);   //发送广播
    } 
    
    发送待机广播
     if (ActivityManagerNative.isSystemReady()) {
        mContext.sendOrderedBroadcastAsUser(mScreenOffIntent, UserHandle.ALL, null,
                mGoToSleepBroadcastDone, mHandler, 0, null, null);
         mContext.sendBroadcastAsUser(mScreenOffIntent_new, UserHandle.CURRENT_OR_SELF);  
    }
    

程序之家二维码

000
评论