Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

java - Android Turn on the screen and start an activity when it's off

I am making an alarm app and I'm at the stage where I need to display the alarm screen (AlarmActivity) as onReceive from BroadcastReceiver is called. Here are the things I need to achieve when it's time for alarm to ring.

  1. When the screen is off, turn on the device and display the alarm screen when it's time.
  2. When the screen is on but the app is off, display the alarm screen.
  3. When the screen is on and the user is in the alarm app, display the alarm screen.

I have tried this code, but it doesn't work. Start activity even when screen is turned off

onCreate of my main activity is..

        setContentView(R.layout.activity_main);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent myIntent1 = new Intent(this, AlarmReceiver.class);
        myIntent1.putExtra("msg", "Wake up Wake up1");
        myIntent1.putExtra("repeat", 0);
        PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, 2571123, myIntent1, PendingIntent.FLAG_UPDATE_CURRENT);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 19);
        calendar.set(Calendar.MINUTE, 48);
        calendar.set(Calendar.SECOND, 00);

        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent1);

It sets the alarm on certain time.

This is my AlarmReceiver class.

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void onReceive(Context context, Intent intent) {
        String msg = intent.getStringExtra("msg");
        Log.d("ALARMONMATE",msg);

        Intent intent1 = new Intent(context, SampleAlarmActivity.class);
        intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent1.putExtra("msg",msg);
        context.startActivity(intent1);

Here is my AlarmActivity.java, the screen I want to display on alarm call.

public class SampleAlarmActivity extends AppCompatActivity {
    private PowerManager.WakeLock wake = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample_alarm);

        Intent intent = getIntent();
        String msg = intent.getStringExtra("msg");
        TextView txt = findViewById(R.id.textView);
        txt.setText(msg);






        PowerManager powerManager = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
        wake = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "App:wakeuptag");
        wake.acquire(1*60*1000);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
            setShowWhenLocked(true);
            setTurnScreenOn(true);
        } else {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
                    WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON|
                    WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
        }
    }
}


If anyone could at least leave a link that I can refer to or concepts that would be helpful in this case, that would be great. I've looked into WakeLock and well over 20 questions about this issue. However, I wasn't too sure if they were outdated or not as majority of them are from over 8 years ago.

question from:https://stackoverflow.com/questions/65883575/android-turn-on-the-screen-and-start-an-activity-when-its-off

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...