[英]BOOT_COMPLETED not working Android
首先,我知道已經問了數百個這樣的問題,但是我已經檢查了一段時間,仍然找不到任何解決方案。
我已經看到這個答案說 BOOT_COMPLETED 不會發送到應用程序,除非用戶首先啟動您的應用程序,在 Android 版本 3.1 之后但我仍然看到一些應用程序正在這樣做,必須有辦法。 我真的需要處理它,否則我也反對在沒有用戶交互的情況下做某事。
所以這是我的 AndroidManifest:
<manifest ... >
<!-- to be activated service on boot is completed -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application ... >
<!-- to receive data when boot completed -->
<receiver
android:name="myPackage.BootReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
提前致謝。
編輯:在我的廣播接收器中沒有什么可看的,但這里需要的是:
package myPackage
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Utils.LogI("BootReceiver", "BootReceiver received!");
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
// Do my stuff
}
}
}
下面這件事對我有用
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application>
<receiver android:name=".BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<service android:name="NotifyingDailyService" >
</service>
BootCompletedReceiver.class
public class BootCompletedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
// TODO Auto-generated method stub
Log.w("boot_broadcast_poc", "starting service...");
context.startService(new Intent(context, NotifyingDailyService.class));
}
}
服務類
public class NotifyingDailyService extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent pIntent, int flags, int startId) {
// TODO Auto-generated method stub
Toast.makeText(this, "NotifyingDailyService", Toast.LENGTH_LONG).show();
Log.i("com.example.bootbroadcastpoc","NotifyingDailyService");
return super.onStartCommand(pIntent, flags, startId);
}
}
這是一個古老而基本的問題,但許多 Android 開發人員現在仍然對這個問題感到困惑,因為他們沒有花時間仔細閱讀文檔
我看到有人分享了一些鏈接並說: “這已經不行了” ,這是完全錯誤和誤解的。
關於這個問題: “我已經看到這個答案說 BOOT_COMPLETED 不會發送到應用程序,除非用戶在 Android 版本 3.1 之后首先啟動您的應用程序” ,請閱讀這些行(來自官方文檔: https://developer.android. com/about/versions/android-3.1.html#launchcontrols )正確理解:
請注意,應用程序的停止狀態與Activity 的停止狀態不同。 系統分別管理這兩個停止狀態。
應用程序在首次安裝但尚未啟動以及由用戶手動停止時(在Manage Applications中)處於停止狀態。 (他們的意思是強制停止應用程序)
這意味着用戶應該在安裝后至少啟動一次應用程序以激活應用程序,然后應用程序可以正常接收來自操作系統的隱式廣播。 (只有一次發射! )
“是否有任何應用程序被安裝並且從未打開過一次?” ,是的,它是垃圾郵件和詐騙應用程序,這種技術可以幫助用戶防止這種情況!
此外,直到現在(Android Oreo 8.0) ,當 Android 限制在 Manifest( https://developer.android.com/about/versions/oreo/background.html#broadcasts )上注冊隱式廣播時,目前仍有幾個廣播不受這些廣播的約束限制。 BOOT_COMPLETED是他們提到的第一個! ( https://developer.android.com/guide/components/broadcast-exceptions.html )
順便說一句,這是我為這個問題找到的最佳解決方案:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".BootReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<!--For HTC devices-->
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
最后,請仔細閱讀文檔並三思而后行代碼一次:3!
一些新的平板電腦和安卓設備默認具有安全應用程序。 有時這些應用程序會鎖定您的自動啟動模式。 這個安全應用程序的一個例子是 MyAsus 管理器。 因此您可以將“允許自動啟動”添加到您的應用程序
對於 Htc 設備,添加com.htc.intent.action.QUICKBOOT_POWERON
<receiver android:enabled="true" android:name=".receivers.BootUpReceiver">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
對於其他仍然像我一樣遇到此問題的人,如果您在具有啟動鎖(pin、模式或其他)的設備上進行調試,則操作系統版本 >= 7.0 需要訂閱android.intent.action.LOCKED_BOOT_COMPLETED
如下圖所示:
<receiver
android:directBootAware="true"
android:name=".BootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT" />
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
您可以在以下鏈接中找到文檔: https ://developer.android.com/training/articles/direct-boot
問題出在設備上。 某些設備只允許內部應用程序接收此操作(例如:Android 5.1)。
您可以將其添加到您的意圖過濾器中作為解決方法
動作android:name="android.intent.action.USER_PRESENT"
這是在用戶解鎖設備后觸發的。
我遇到的問題是,當我從 Android 6.0.1 面板關閉電源時, BOOT_COMPLETED
和QUICKBOOT_POWERON
並不總是觸發我的意圖。 我在網上搜索了很長時間,通過在清單文件中添加QUICKBOOT_POWEROFF
找到了解決方案。
也可以看看:
如果您達到了這個答案並且其他答案似乎都不起作用,請仔細檢查清單中的操作字符串。 如果您在action
標簽中寫入了錯誤的字符串,AndroidStudio 不會顯示任何錯誤。 不要混淆代碼中常量的名稱,即ACTION_BOOT_COMPLETED
與所述常量的值,即清單中的常量,等於android.intent.action.BOOT_COMPLETED
TL;DR 檢查清單中是否有此內容:
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
而不是這個:
<intent-filter>
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
</intent-filter>
對於解決方法,您需要創建 NotificationListener 服務
如果設備 >= Build.VERSION_CODES.JELLY_BEAN_MR2 並且具有華為設備中的電池優化選項,那么您必須請求 NotificationListener 權限並按照以下代碼創建 NotificationListener 服務,然后您將在接收器中獲得 BOOT_COMPLETED
<receiver
android:name=".TestRes"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1">
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<action android:name="android.intent.action.USER_PRESENT"/>
<action android:name="android.intent.action.REBOOT"/>
</intent-filter>
</receiver>
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public class DevAppNotificationListener extends NotificationListenerService {
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
// super.onNotificationPosted(sbn);
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
// super.onNotificationRemoved(sbn);
}
}
static boolean CheckNotificationLisPermission(Context context)
{
return NotificationManagerCompat.getEnabledListenerPackages (context).contains(context.getApplicationContext().getPackageName());
}
Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
context.startActivityForResult(intent, callBackResultIntent);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.