我正在做一个应用程序,它使用地理围栏来跟踪我们的事件,我想创建一个警报管理器,它每天都被激活并为当天的事件(从数据库中获取)添加地理围栏。我需要创建一个使用地理围栏的活动,在这个活动中我放了BroadcastReceiver。它似乎根本不起作用。我甚至没有从这些活动获得任何日志。我不知道为什么,但我认为在Manifest中可能有错。我很感激有人会看到这个。
Class MyScheduler:
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);
}
BroadcastReceiver的任务:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<activity android:name=".MyScheduler"/>
<activity android:name=".Tasks"/>
<receiver android:name=".Tasks" >
</receiver>
部分清单:
CollapsingToolbarLayout
我可以补充说,地理围栏的所有内容都运行良好,所以我没有把地理围栏代码放在上面,因为它更容易阅读。希望有人能够提供帮助! :)
答案 0 :(得分:2)
您正在创建PendingIntent以发送广播,但您指定了一个Activity类(Tasks)。如果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();
}
}
并指定清单中的挂起意图和接收者声明的类名称如下所示:
<receiver android:name=".TasksReceiver" />