【发布时间】:2017-05-22 23:08:24
【问题描述】:
我正在做一个应用程序,它使用geofences 来跟踪我们的事件,我想创建一个警报管理器,它每天激活并为当天的事件添加地理围栏(取自数据库)。
我需要创建一个活动来使用地理围栏,在这个活动中,我输入了BroadcastReceiver。似乎它根本不起作用。我什至没有从这些活动中得到任何日志。我不知道为什么,但我想 Manifest 中可能有问题。如果有人能看到这个,我将不胜感激。
类 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);
}
}
使用 BroadcastReceiver 的任务:
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);
}
以及清单的一部分:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<activity android:name=".MyScheduler"/>
<activity android:name=".Tasks"/>
<receiver android:name=".Tasks" >
</receiver>
我可以补充一点,所有带有地理围栏的东西都运行良好,所以我没有把地理围栏代码放在上面,因为它更容易阅读。希望有人能提供帮助! :)
【问题讨论】:
标签: android broadcastreceiver scheduled-tasks alarmmanager geofencing