我正在开发一个应用程序,它应该将下一个闹钟时间从我的手机发送到我的覆盆子pi。 我设法得到下一个警报的时间,现在我需要一些方法来在警报改变时运行一些代码,我发现更改警报会发送广播。我的问题是,如何设置一个只接收警报更改广播时才执行代码的接收器?
编辑:
package com.dreamdesigns.luke.alarmport;
import android.os.Bundle;
import android.util.Log;
import android.support.v4.content.LocalBroadcastManager;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.BroadcastReceiver;
import android.content.Context;
public class RecieverActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
...
// Register to receive messages.
// We are registering an observer (mMessageReceiver) to receive Intents
// with actions named "custom-event-name".
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("custom-event-name"));
}
// Our handler for received Intents. This will be called whenever an Intent
// with an action named "custom-event-name" is broadcasted.
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
Log.d("receiver", "Got message: " + message);
}
};
@Override
protected void onDestroy() {
// Unregister since the activity is about to be closed.
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
}