为什么我的广播接收器在收到警报后仍无法启动?

时间:2019-08-11 00:39:50

标签: java android broadcastreceiver

我要尝试的是在使用电话后立即向用户打招呼。 AlarmReceiver在凌晨5点关闭并设置布尔值is_ready_to_speak 真实。意图操作USER_PRESENT调用广播屏幕接收器后 或SCREEN_ON,只要is_ready_to_speak设置为true,就应该 使用我的PASpeak类向用户打招呼(有效,没问题) 所有这些代码都可以在模拟器上正常运行,并使用ADB触发 USER_PRESENT广播。但是我无法在手机上使用它。 警报触发,但ScreenReceiver从未触发。

package com.pa.blah.positiveaffirmations;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

import java.util.Calendar;

public class PAService extends Service {

    static boolean is_ready_to_speak = false;
    PendingIntent pendingIntent;

    public PAService() {

    }

    public class ScreenReceiver extends BroadcastReceiver {S

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("RECEIVED BROADCAST","Sometype of Broadcast received");
            if(is_ready_to_speak)
            {
                PASpeak speakyo = new PASpeak();
                speakyo.sayit(context.getApplicationContext(),"This should go off after user present,only after the alarmreceiver has been triggered)");
               is_ready_to_speak = false;
            }


        }
    }


    public static class AlarmReceiver extends BroadcastReceiver {

        public AlarmReceiver()
        {
            Log.d("AlarmReceiver func called","alarm receiver func called");
        }
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("RECEIVED BROADCAST", "Sometype of ALARM Broadcast received");
            PASpeak speakyo = new PASpeak();
            speakyo.sayit(context.getApplicationContext(),"ALARM ALARM ALARM");
            is_ready_to_speak = true;
        }

    }



    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        IntentFilter filter = new IntentFilter(Intent.ACTION_USER_PRESENT);
        filter.addAction(Intent.ACTION_SCREEN_ON);
        BroadcastReceiver mReceiver = new ScreenReceiver();
        registerReceiver(mReceiver, filter);


        Intent alarmIntent = new Intent(PAService.this, AlarmReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(PAService.this, 1, alarmIntent, 0);

        AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        // Set the alarm to start at 5:00 AM
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 5);`enter code here`
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        Calendar current = Calendar.getInstance();

        long curTime = current.getTimeInMillis();
        long alarmTime = calendar.getTimeInMillis();

        if (alarmTime >= curTime){
            manager.setRepeating(AlarmManager.RTC_WAKEUP,
                    alarmTime, AlarmManager.INTERVAL_DAY, // for repeating
                    // in every 24
                    // hours
                    pendingIntent);
        }else{
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            alarmTime = calendar.getTimeInMillis();
            manager.setRepeating(AlarmManager.RTC_WAKEUP,
                    alarmTime, AlarmManager.INTERVAL_DAY, // for repeating
                    // in every 24
                    // hours
                    pendingIntent);
        }

        Log.d("ALARM", "ALARM set");

        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

Android Manifest.xml

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

    <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>

        <receiver android:name=".PAService$ScreenReceiver">
            <intent-filter>
                <action android:name="android.intent.action.USER_PRESENT" />
                <action android:name="android.intent.action.SCREEN_ON" />
                <!-- <action android:name="android.intent.action.SCREEN_OFF" /> -->
                <!--
                <action android:name="android.intent.action.SCREEN_ON" />
                <action android:name="android.intent.action.ACTION_SHUTDOWN" />
                <action android:name="android.intent.action.CONFIGURATION_CHANGED" />
                <action android:name="android.net.nsd.STATE_CHANGED" />
                -->
            </intent-filter>
        </receiver>

        <receiver android:name=".PAService$AlarmReceiver" />

        <receiver android:name=".PAAppWidget">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/paapp_widget_info" />
        </receiver>

        <service
            android:name=".PAService"
            android:enabled="true"
            android:exported="true"></service>
    </application>

</manifest>

0 个答案:

没有答案