Android9.0 监听开机广播
軟件版本:9.0Go
硬件平臺:MTK8175
近日,需要在如上平臺監(jiān)聽開機廣播,Android高版本限制了開機廣播監(jiān)聽的權(quán)限,這次我添加到了SystemUI中,方法如下:
進入SystemUI源碼目錄,添加src/com/android/systemui/media/GeiniBroadcastReceiver.java
類,內(nèi)容如下:
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*? ? ? http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.systemui.media;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Slog;
//import com.android.server.policy.PhoneWindowManager;
public class GeiniBroadcastReceiver extends BroadcastReceiver {
? ? private static final boolean DBG = true;
? ? private static final String TAG = "======";
? ? @Override
? ? public void onReceive(Context context, Intent intent) {
? ? ? ? if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
? ? ? ? ? ? if (DBG) {
? ? ? ? ? ? ? ? Slog.e(TAG, "onReceive BOOT_COMPLETED_ACTION windowmanager:");
? ? ? ? ? ? }? ?
? ? ? ? }? ?
? ? }? ?
}
第二部,在Manifest靜態(tài)注冊廣播:
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -270,6 +270,13 @@
?? ? ? ? ? ? android:process=":screenshot"
?? ? ? ? ? ? android:exported="false" />
+? ? ? ? <receiver android:name=".media.GeiniBroadcastReceiver">
+? ? ? ? ? ? <intent-filter>
+? ? ? ? ? ? ? ? <action android:name="android.intent.action.BOOT_COMPLETED"></action>
+? ? ? ? ? ? ? ? <category android:name="android.intent.category.DEFAULT" />
+? ? ? ? ? ? </intent-filter>
+? ? ? ? </receiver>
+
如此便添加完成,可在第一步的onReceive方法中添加自己的目標代碼,處理一些邏輯。。。
總結(jié)
以上是生活随笔為你收集整理的Android9.0 监听开机广播的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 荀氏族谱
- 下一篇: Android项目:音乐播放器