class AlarmReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Toast.makeText(context,"Ready to wake up?", Toast.LENGTH_LONG).show()
val myIntent = Intent(context,AlarmActivity::class.java)
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(myIntent)
}
}
顯現:
<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/Theme.QuickNap">
<activity android:name=".AlarmActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.NoActioBar"
android:windowSoftInputMode="adjustResize"></activity>
<!-- <receiver-->
<!-- android:name=".AlarmReceiver"-->
<!-- android:process=":remote" />-->
<receiver android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true"
android:process=":remote">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.REBOOT"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.NoActioBar"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
主要活動:
onTimer = true;
moonImage.setColorFilter(null)
val mIntent = Intent(this, AlarmReceiver::class.java)
val mPendingIntent = PendingIntent.getBroadcast(this, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val calendar = Calendar.getInstance()
calendar.add(Calendar.SECOND, minuteSet)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,calendar.timeInMillis,mPendingIntent)
}
else
{
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, mPendingIntent)
}
嗨,我正在嘗試使用 kotlin 創建一個警報 android 應用程式。所以我創建了一個接收器,在那個接收器中我打開了一個活動,在那個活動中我正在播放聲音。
此代碼在模擬器上按預期作業正常。但在設備上(Redmi Note 7 pro、MIUI OS)。它僅在螢屏打開且應用程式打開時有效。關于如何確保在呼叫此接收器時打開螢屏的任何想法?
我也是 android 開發的新手,所以 ELI5. 泰
uj5u.com熱心網友回復:
轉到設定/應用程式/管理應用程式并選擇您的應用程式,然后選擇省電模式并確保它處于“無限制”狀態,以便您的應用程式可以在后臺運行而不受任何限制。
如果它有效,你應該教用戶這樣做。寫一個意圖打開設定等等。
uj5u.com熱心網友回復:
您無法將應用程式置于前臺的原因有很多。讓我們分解它并相應地解決它:
1- Stock OS 限制: Android 以對后臺任務的限制越來越多而聞名。由于其“電池優化”演算法,MIUI甚至以更多限制超越了這一點。首先,請確保您通知用戶如果啟用了 MIUI 電池優化,則該應用程式不能在后臺運行或被帶到前臺。因此,通過向用戶提供一鍵式意圖,讓用戶能夠進入應用選項中的省電選項。可悲的是,沒有程式化的解決方法。您可以在此處查看執行節電政策的制造商串列:不要殺死我的應用程式!
小米并不是您唯一需要擔心的制造商。
2- Android 10 (API 29) 限制:從這個特定的 API 開始,應用程式不能再從后臺跳轉到前臺。谷歌開始對許多權限和功能施加大量限制。請在此處閱讀有關必須滿足哪些條件才能啟動在后臺運行的應用程式的更多資訊:啟動活動的限制 - API 29
您可以做什么:您可以使用不同的標志和類別以及要添加到清單中的引數。例如,如果您尚未添加此權限,請確保現在執行以下操作:
QUOTED FROM THE LINK ABOVE:
...
The app should be granted the SYSTEM_ALERT_WINDOW permission by the user.
...
您也可以嘗試添加不同的標志和類別,這可能行不通,但您仍然可以嘗試擺弄以下兩行:
intent.addCategory(Intent.CATEGORY_LAUNCHER) //You can always add and remove categories
intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
你最好的解決方案:你總是可以檢查一個開源專案,看看他們是如何處理這個特定問題的。我最喜歡的必須是這個:GitHub 上的 AlarmClock FOSS,作者 yuriykulikov
祝你好運。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/342581.html
