Android:应用程序中的Alarm.manager触发的弹出对话框应该出现在屏幕上,即使该应用程序未处于活动状态或根本未运行

um6iljoc  于 2023-04-04  发布在  Android
关注(0)|答案(1)|浏览(125)

我正在开发一个Android应用程序,它使用AlarmManager,PendinIntent和BroadcastReceiver来触发弹出对话框(使用Dialogfragment创建)。只要此应用程序处于活动状态(在屏幕上),它就可以正常工作。
我需要这个对话框弹出,如果另一个应用程序也是活动的,更好地说,如果没有应用程序也是活动的。类似于闹钟应用程序。
什么是正确的方式做到这一点?
启动AlarmManager的方法:

logButton.setOnClickListener(new Button.OnClickListener(){

    @Override
    public void onClick(View arg0) {

        Intent myIntent = new Intent(getBaseContext(), ScheduledReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(),0, myIntent, PendingIntent.FLAG_IMMUTABLE);
        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, 10);
        long interval = 3600 * 1000;
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pendingIntent);
            }});

BroadcastReceiver:

public class ScheduledReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

                Intent scheduledIntent = new Intent(context, TransparentAlertActivity.class);
                scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(scheduledIntent);
    }
}

TransparentAlertActivity(对话框在前面时显示为透明),在其onCreate()中调用dialog()以显示扩展DialogFragment的对话框:

public class TransparentAlertActivity extends ActionBarActivity {

    public static Intent getIntent(@NonNull Context context) {
        return new Intent(context, MedicationListActivity.class);
    }

    private MedicationListViewModel viewModel;
    private boolean isActive;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transparent_alert);
        viewModel = new ViewModelProvider(this).get(MedicationListViewModel.class);
        Log.w("myApp", "in onCreate before inActive() called isActive =" +isActive);
        isActive();
        dialog();
    }

    public void isActive(){
        isActive = viewModel.isMedListActive();
        Log.w("myApp", "in isActiveMethod isActive =" +isActive);
    }

    private void dialog(){
        if(isActive){
            Log.w("myApp", "in dialog and isActive =" +isActive);
            FragmentManager fm = getSupportFragmentManager();
            MedicationDetailsDialogFragment medicationDetailsDialogFragment =
                    MedicationDetailsDialogFragment.createInstance("You need to take your medication now!", "OK", "NEVER MIND");

            medicationDetailsDialogFragment.setOption1ClickListener(dialogView -> {
                overridePendingTransition(0, 0);
                startActivity(MedicationListActivity.getIntent(this));
                overridePendingTransition(0, 0);
            });
            medicationDetailsDialogFragment.setOption2ClickListener(dialogView -> {
                finish();
            });

           medicationDetailsDialogFragment.show(fm, "Medication");
        }
    }
}
9gm1akwq

9gm1akwq1#

试试这些:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

并在manifest中添加此权限:

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

来源:https://stackoverflow.com/a/31440994/14187529

相关问题