【问题标题】:Android: How to get my app to open on screen from Broadcast Receiver and alarmAndroid:如何让我的应用程序从广播接收器和警报在屏幕上打开
【发布时间】:2020-11-06 02:58:01
【问题描述】:

我希望我的应用定期在屏幕上打开(即使文档说不推荐)进行认知实验。
在主活动中,用户点击一个按钮来安排下一次测试,然后关闭应用程序。我有使用alarmManager 调用BroadcastReceiver 的按钮回调,这似乎正在工作。我按下按钮,关闭应用程序,X 秒后,接收器发出声音并记录。
但我无法让接收器强制应用程序再次在屏幕上打开,就像我的闹钟和秒表一样。 (我需要这个,而不仅仅是一个通知,因为用户需要尽可能少地点击测试,他们每天会做 50 次......)奇怪的是,如果我不关闭按下按钮进行调度后应用程序,然后我确实看到应用程序被重新启动:屏幕重绘自身,我的来自onCreate 的日志语句运行。
但是如果我在按下按钮后关闭它,我会听到接收器中的声音,但我的应用程序没有在屏幕上打开。如果 Activity 已经在前台打开,接收器似乎只能唤醒 Main Activity。
我不知道我错过了什么!谢谢

代码:

按钮回调摘录,来自主要活动(似乎有效)

// cb to put app to sleep until next round
    public void onDone(View view){
        Log.i(TAG, "onDone(). setting alarm");
        int i = 5;  // wake in 5 seconds
        Intent intent = new Intent(this, WakeupReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                this.getApplicationContext(), 234324243, intent, 0);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                + (i * 1000), pendingIntent);
    }

接收者类:(执行正常)

public class WakeupReceiver extends BroadcastReceiver {

    private static final String TAG = "INFO";

    MediaPlayer mp;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "WakeupReceiver running: ");
        mp=MediaPlayer.create(context, R.raw.wake);
        mp.start();
        Intent i = new Intent(context, MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
        Log.i(TAG, "...sent intent");
    }
}

我的清单:

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

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

    <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/Theme.PitchExperiment"
        >
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="WakeupReceiver">
        </receiver>
    </application>

</manifest>

【问题讨论】:

    标签: android broadcastreceiver alarmmanager


    【解决方案1】:

    我建议你用service.startForeground(NOTIFICATION_ID, notification) 运行一个服务,这样你的服务就不会被安卓系统杀死。在服务中注册您的WakeupReceiver 并且每次onReceive 您可以使用service 的上下文调用startActivity

    【讨论】:

      猜你喜欢
      • 2012-03-17
      • 2023-04-04
      • 1970-01-01
      • 2018-04-17
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多