警报后触发活动(RTC_WAKEUP)但屏幕仍为黑色

时间:2018-06-17 17:25:32

标签: android alarmmanager

我正在设置一个关于如何对Android中的警报做出反应的骨架应用。当手机处于唤醒状态时,一切正常,当手机处于睡眠状态时,警报会触发(我甚至可以激活振动器),但屏幕保持关闭状态(完全黑色)。我不能破译为什么。欢迎任何帮助!

这是MainActivity中设置alarmManager的代码片段(E1是一个EditText,要求用户选择秒来触发警报):

public void startAlarm(View view) {
    EditText E1 = (EditText) findViewById(R.id.et1);
    int i = Integer.parseInt(E1.getText().toString());
    Intent intent = new Intent(this, AlarmBroadcastReceiver.class);
    PendingIntent pending_intent = 
    PendingIntent.getBroadcast(this.getApplicationContext(),
        98989898, intent, 0); // 98989898 : some big number
    AlarmManager alarmManager = (AlarmManager) 
        getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, 
        System.currentTimeMillis() + i*1000, pending_intent);
    Toast.makeText(this, "Alarm set in: " + i + " seconds", 
    Toast.LENGTH_LONG).show();
}

alarmBroadcastReceiver会接收警报。它本身没有任何作用,它立即转移到UserResponseActivity:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AlarmBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // We're creating a new intent that's going to start the UserResponseActivity
        Intent in = new Intent(context, UserResponseActivity.class);
        // This boolean just makes it easier to check if the Activity has been started from
        // this class
        in.putExtra("lock", true);
        // You need to add this to your intent if you want to start an Activity fromm a class
        // that is not an Activity itself
        in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        // Now we just start the Activity
        context.startActivity(in);
    }
}

然后UserResponseActivity是一个普通的活动,目前有来自Android Studio的样板画面。稍后我计划添加一些请求用户响应的代码。创建后,它会显示一个吐司并在短时间内激活振动器:

import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;

public class UserResponseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_response);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        /* Show a success toast*/
        Toast.makeText(this, "Alarm Started", Toast.LENGTH_LONG).show();
        /* Vibrate shortly */
        Vibrator vibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

}

如果您需要它,我的清单中的代码行启用了警报接收器和振动器服务:

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

<receiver android:name=".AlarmBroadcastReceiver" />

如上所述,如果在手机唤醒时触发警报,一切正常:屏幕上显示UserResponseActivity,振动器熄灭。 但是,如果在手机处于睡眠状态时触发警报,则振动器会熄灭(因此手机会被唤醒并调用UserResponseActivity),但屏幕会保持黑屏。当我随后打开手机时,UserResponseActivity就在那里。

我需要做些什么才能让UserResponseActivity 开启屏幕

谢谢!

ADDED(在VicJordan回复后):

@VicJordan:我将你的一段代码粘贴到了UserResponseActivity.java的onCreate中,如下所示,但得到两个错误,我无法找到我在这两行中做错了什么:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
        activity.setTurnScreenOn(true);

a)Android Studio无法解析符号'O_MR1' b)它可以在第二行解决'活动'。我认为改变'this'的活动可以解决问题,但不是。我真的无法想出另一种方式来引用我所在的活动

public class UserResponseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_response);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        // Turn on the screen
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
            activity.setTurnScreenOn(true);
        } else {
            final Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        }

        /* Show a success toast*/
        Toast.makeText(this, "Alarm Started", Toast.LENGTH_LONG).show();
        /* Vibrate shortly */
        Vibrator vibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

你有什么建议吗?谢谢!

1 个答案:

答案 0 :(得分:1)

要在“活动”中打开屏幕,您可以使用WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,但API级别27已弃用alias reset rm -rf composer.lock node_modules package-lock.json vendor composer install npm install gulp composer validate alias update rm -rf node_modules vendor composer install npm install composer update npm update reset 标志,因此您可以从API级别27开始使用Activity.setTurnScreenOn(true)

下面是代码:

FLAG_TURN_SCREEN_ON

查看此官方信息以获取更多信息:https://developer.android.com/training/scheduling/wakelock