我正在做一个应用程序,它使用 geofences跟踪我们的事件,我想创建一个警报管理器,它每天激活并为当天的事件添加地理围栏(从数据库中获取)。
我需要创建一个 Activity 来使用地理围栏,在此 Activity 中,我输入 BroadcastReceiver 。似乎根本不起作用。我什至没有从这些 Activity 中获得任何日志。我不知道为什么,但我认为 list 中可能有问题。如果有人能看一下这个,我将不胜感激。
MyScheduler 类:
public class MyScheduler extends Activity {
protected static final String TAG = "MyScheduler";
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "onCreate");
super.onCreate(savedInstanceState);
setRecurringAlarm(this);
}
private void setRecurringAlarm(Context context) {
Log.i(TAG, "setReccuringAlarm");
Calendar updateTime = Calendar.getInstance();
updateTime.setTimeZone(TimeZone.getTimeZone("GMT+1:00"));
updateTime.set(Calendar.HOUR_OF_DAY, 15);
updateTime.set(Calendar.MINUTE, 10);
Intent intent = new Intent(context, Tasks.class);
PendingIntent recurringDownload = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager) this.getSystemService(
Context.ALARM_SERVICE);
alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP,
updateTime.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, recurringDownload);
}
}
广播接收器的任务:
public class Tasks extends Activity
implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, ResultCallback<Status> {
Context context;
final public static String ONE_TIME = "onetime";
EventDAO eventDAO;
protected static final String TAG = "MyGeofence";
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String today = dateFormat.format(calendar.getTime());
List<Event> eventsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
mGeofenceList = new ArrayList<Geofence>();
mGeofencePendingIntent = null;
mSharedPreferences = getSharedPreferences(Constants.SHARED_PREFERENCES_NAME,
MODE_PRIVATE);
mGeofencesAdded = mSharedPreferences.getBoolean(Constants.GEOFENCES_ADDED_KEY, false);
buildGoogleApiClient();
}
/// geofences mathods, not needed
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(ONE_TIME, "Executed Tasks.Java File");
//Some task here for every morning
eventDAO = new EventDAO(context);
eventsList = eventDAO.getComingEvents(today, today);
for( Event event : eventsList){
String place = event.getEndLocalisationX();
Log.i(ONE_TIME, place);
Double lat = Double.parseDouble(event.getEndLocalisationY());
Double lng = Double.parseDouble(event.getEndLocalisationX());
MY_PLACES.put(place, new LatLng(lat, lng));
}
populateGeofenceList();
addGeofences();
}
};
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(broadcastReceiver);
}
以及 list 的一部分:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<activity android:name=".MyScheduler"/>
<activity android:name=".Tasks"/>
<receiver android:name=".Tasks" >
</receiver>
我可以补充一点,所有带有地理围栏的东西都运行良好,所以我没有将地理围栏代码放在上面,因为它更容易阅读。希望有人能够提供帮助! :)
最佳答案
您正在创建一个 PendingIntent 来发送广播,但您指定了一个 Activity 类(任务)。如果 Android 允许的话,您不应该设置闹钟来用 UI 打断用户。您应该将广播接收器移至外部类,例如 TasksReceiver:
public class TasksReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(ONE_TIME, "Executed Tasks.Java File");
//Some task here for every morning
EventDAO eventDAO = new EventDAO(context);
List<Event> eventsList = eventDAO.getComingEvents(today, today);
for( Event event : eventsList){
String place = event.getEndLocalisationX();
Log.i(ONE_TIME, place);
Double lat = Double.parseDouble(event.getEndLocalisationY());
Double lng = Double.parseDouble(event.getEndLocalisationX());
MY_PLACES.put(place, new LatLng(lat, lng));
}
populateGeofenceList();
addGeofences();
}
}
并指定待处理 Intent 的类名和 list 中的接收器声明如下所示:
<receiver android:name=".TasksReceiver" />
关于android - 警报管理器不适用于具有 BroadcastReceiver 的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41535313/