Anand Kumar Jha
Anand Kumar Jha

Reputation: 1

Broadcast Receiver App Activity Launching

I am making an android alarm app, I want to show alarm activity when alarm time is reached. Problem is that the activity is opening in the app but the app is not launched automatically.

I want to make my alarm activity come on foreground when the alarm time is reached whether the app is open or not.

The manifest file

`<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"package="com.example">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

<application
    //Removed unwanted parts

    <receiver
        android:name=".classes.AlarmReceiver"
        android:enabled="true"
        android:exported="true"
        android:permission="TODO">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

</application>
</manifest>

This is the Alarm Activity which I want to appear on time.

class AlarmPage : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)supportRequestWindowFeature(Window.FEATURE_NO_TITLE);window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,)

setContentView(R.layout.activity_alarm_page)

    val mediaPlayer = MediaPlayer.create(this, R.raw.main_alarm)
    mediaPlayer?.isLooping = true
    mediaPlayer?.start()

    findViewById<Button>(R.id.stop_button).setOnClickListener {
        mediaPlayer?.stop()
        mediaPlayer?.release()
        finish()
    }
}
}`

This is the Alarm Receiver class

`class AlarmReceiver: BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent?) {

    val launchIntent = Intent(context,AlarmPage::class.java)
    launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)
    context.startActivity(launchIntent)
}

}`

The Alarm Receiver class opens the Activity at the time but when I am not using my app or my app is in background the app doesn't opens automatically at the time, I have to manually open the app to see the activity is opened or not.

Upvotes: -1

Views: 24

Answers (1)

Anand Kumar Jha
Anand Kumar Jha

Reputation: 1

Actually I myself found the answer, that is everything was correct and I just had to manually allow my application to "Draw or allow over other apps" permission.

That is mentioned in manifest as

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

Upvotes: 0

Related Questions