发生警报时,BroadcastReceiver不会触发

时间:2016-04-28 13:25:59

标签: android broadcastreceiver android-alarms

我正在开发一个应用程序,当触发闹钟时,它会在后台执行某些操作。现在它只是一个祝酒词。 在运行时我没有得到任何错误,但是当警报响起时,广播接收器没有被调用。 我错过了什么?

AlarmReceiver:

public class AlarmReceiver extends BroadcastReceiver {
        public static final String ALARM_ALERT_ACTION = "com.android.deskclock.ALARM_ALERT";
        @Override
        public void onReceive(Context context, Intent intent) {
            {
                Intent alarm = new Intent(ALARM_ALERT_ACTION);
               // context.registerReceiver(this, alarm);
                String action = intent.getAction();
                if (action.equals(ALARM_ALERT_ACTION))
                {
                    // for play/pause mediaplayer
                    CharSequence text = "Hello toast!";
                    int duration = Toast.LENGTH_SHORT;

                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();
                }
            }
        }
    }

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.noel.alarmnotification">
    <uses-permission android:name="com.android.alarm.permission."/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>
            <receiver android:name=".AlarmReceiver" android:process=":remote">
                <intent-filter>
                    <action android:name="com.android.deskclock.ALARM_ALERT"/>
                </intent-filter>
            </receiver>
        </activity>
    </application>

</manifest>

修改

MainActivity:

public class MainActivity extends AppCompatActivity {
    public static final String ALARM_ALERT_ACTION = "com.android.deskclock.ALARM_ALERT";
    public static final String ALARM_SNOOZE_ACTION = "com.android.deskclock.ALARM_SNOOZE";
    public static final String ALARM_DISMISS_ACTION = "com.android.deskclock.ALARM_DISMISS";
    public static final String ALARM_DONE_ACTION = "com.android.deskclock.ALARM_DONE";


    private BroadcastReceiver mReceiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            String action = intent.getAction();
            if (action.equals(ALARM_ALERT_ACTION) || action.equals(ALARM_DISMISS_ACTION) || action.equals(ALARM_SNOOZE_ACTION) || action.equals(ALARM_DONE_ACTION))
            {
                // for play/pause mediaplayer

            }
        }
    };


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        IntentFilter filter = new IntentFilter(ALARM_ALERT_ACTION);
        filter.addAction(ALARM_DISMISS_ACTION);
        filter.addAction(ALARM_SNOOZE_ACTION);
        filter.addAction(ALARM_DONE_ACTION);
        registerReceiver(mReceiver, filter);
    }
}

当我以编程方式创建寄存器时,一旦应用关闭,它就不会广播警报