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)
}
}
Manifest:
<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>
MainActivity:
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)
}
hi, i am trying to create a alarm android app using kotlin .
so i created a Reciever, in that reciever i am switching on a activity and in that activity i am playing Sound.
this code works fine as intended on emulator. but on device (Redmi Note 7 pro, MIUI OS). it only works when screen is on and if the app is open.
any idea on how to make sure it switches the screen on when this receiver is invoked?
also i am new to android development, so ELI5. ty
2
Answers
Go to Settings/Apps/manage apps and select your application then select battery saver and make sure it is on "No restrictions" so your app can run in the background without any restrictions.
If it worked, you should teach the user to do that. write an intent to open the settings and so on.
There are plenty of reasons why you cannot bring the app to the foreground. Let’s break it down and solve it accordingly:
1- Stock OS Restrictions: : Android is known for the increasing restriction on background tasks. MIUI even surpassed that with more restriction due to their "Battery Optimization" algorithm. First of all, make sure you inform the user that the app CANNOT run in the background or be brought to the foreground if the MIUI Battery Optimization is enabled. Therefore, give the user the ability to go to the Battery Saver option in app’s options by giving him a one-click intent. Sadly, there is no programmatic work-around. You can see here a list of manufacturers that enforce their battery saving policies: Don’t kill my app!
Xiaomi is not the only manufacturer you have to worry about.
2- Android 10 (API 29) Restrictions: Starting from this particular API, apps can NO LONGER jump to the foreground from the background. Google started putting a lot of restrictions on a lot of permissions and features. Please read more here about which conditions have to be met in order to be able to launch the app that is running in the background: Restrictions on starting activities – API 29
What can you do: You can mess around with different flags and categories as well as parameters to add to the manifest. For example, if you haven’t added this permission, make sure you do now:
You can also try to add different flags and categories, this MAY WELL NOT work, but you can still try and fiddle especially with these two lines:
Your best solution yet: You can always inspect an open-source project and see how they dealt with this particular issue. One of my favorites has to be this one: AlarmClock FOSS on GitHub by yuriykulikov
Best of luck.