I am developing an app which triggers the following steps.
- Uses AlarmManager to trigger a BroadcastRecevier 5 seconds after launch.
- The BroadcastReceiver then sets off an Activity through an Intent.
- Within that Activity, an Alert Dialog is launched.
When I launch the application on my Motorola Moto E6 (Android 9), the Alert Dialog that is triggered looks like the following:
However, I want the AlertDialog to look like the following dialog (how would I go about making it look like that):
Because the Activity cannot have a UI (it should just show a dialog over the screen), I believe it needs to have the following definition in AndroidManifest.xml, in which the theme is specified as translucent:
<activity
android:name=".AlarmDialog"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:screenOrientation="fullSensor"
android:showOnLockScreen="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alertdialog">
<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/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AlarmDialog"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:screenOrientation="fullSensor"
android:showOnLockScreen="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
MainActivity.kt:
package com.example.alertdialog
import android.app.Activity
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private fun generateAlarmPendingIntent(context: Activity): PendingIntent? {
val intent = Intent(context, AlarmReceiver::class.java)
val alarmId: Int = (Math.random() * (100 - 1 + 1) + 1).toInt();
return PendingIntent.getBroadcast(context, alarmId, intent, 0)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Trigger an alarm 5 seconds after the app launches
val alarmManager = applicationContext.getSystemService(Context.ALARM_SERVICE)
as AlarmManager
alarmManager.setExactAndAllowWhileIdle(
AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + 5000,
generateAlarmPendingIntent(this)
)
}
}
AlarmReceiver.java:
package com.example.alertdialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context k1, Intent k2) {
// Trigger the Activity which will launch the Alarm's dialog
Intent i = new Intent(k1, AlarmDialog.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
k1.startActivity(i);
}
}
AlarmDialog.java:
package com.example.alertdialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.WindowManager;
public class AlarmDialog extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
// Construct and display the alarm's alert dialog
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Title");
alertDialog.setMessage("Message");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
}