我正在制作一个应用程序。作为位置任务提醒。当用户到达特定位置时,如果有与该位置相关的任务,他/她将获得该任务的其余部分。
我已经完成了一半以上的应用程序。当用户在该位置时,我可以显示任务。但它是手动的。用户必须手动打开 d app n chk。
Notifications
我的问题是:当用户到达特定位置时如何弹出该任务。这里是Broadcast Receiver
必需的吗?以及将其用作Service
这是我的代码。它仅用于在列表中显示位置任务。通过单击 ToggleButton,它仅将那些任务显示到附近 1 公里(LOCATION_FILTER_DISTANCE)的位置。在这如何使用 NotificationManger.Also 我认为服务是必需的,因为当前位置没有被更新......!
public class ViewTasksActivity extends ListActivity implements LocationListener {
protected static final long LOCATION_FILTER_DISTANCE = 1000;
private Button addButton;
private Button removeButton;
private TaskManagerApplication app;
private TaskListAdapter adapter;
private LocationManager locationManager;
private Location latestLocation;
private TextView locationText;
private ToggleButton localTasksToggle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setUpViews();
app=(TaskManagerApplication)getApplication();
adapter=new TaskListAdapter(app.getCurrentTasks(),this);
setListAdapter(adapter);
setUpLocation();
LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f, this);
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
latestLocation=location;
String locationString=String.format(" @%f, %f ,%s",
location.getLatitude(),
location.getLongitude(),
location.getProvider());
locationText.setText(locationString);
Toast.makeText(getApplicationContext(),locationString, Toast.LENGTH_LONG).show();
}
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER , 60,5,this);
locationManager.removeUpdates(this);
adapter.forceReload();
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
adapter.toggleTaskCompleteAtPosition(position);
Task t=adapter.getItem(position);
app.saveTask(t);
}
protected void removeCompletedTasks() {
Long[] ids=adapter.removeCompletedTasks();
app.deleteTasks(ids);
}
private void setUpLocation() {
locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER , 60,5,this);
}
private void setUpViews() {
// TODO Auto-generated method stub
addButton=(Button)findViewById(R.id.add_button);
removeButton=(Button)findViewById(R.id.remove_button);
locationText=(TextView)findViewById(R.id.location_text);
localTasksToggle=(ToggleButton)findViewById(R.id.show_local_task_toggle);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i=new Intent(ViewTasksActivity.this,AddTaskActivity.class);
startActivity(i);
}
});
removeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
removeCompletedTasks();
}
});
localTasksToggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showLocalTasks(localTasksToggle.isChecked());
}
private void showLocalTasks(boolean checked) {
if(checked){
adapter.filterTasksByLocation( latestLocation,LOCATION_FILTER_DISTANCE);
}else
{
adapter.removeLocationFilter();
}
}
});
}
public void onLocationChanged(Location location) {}
/* latestLocation=location;
String locationString=String.format(" @ %f,%f +/-% fm ",
location.getLatitude(),
location.getLongitude(),
location.getAccuracy());
locationText.setText(locationString);
}
下面的另一个活动:
public class TaskListAdapter extends BaseAdapter {
private ArrayList<Task> tasks;
private Context context;
private ArrayList<Task> filterTasks;
private ArrayList<Task> unfilteredTasks;
public TaskListAdapter(ArrayList<Task> tasks, Context context) {
this.tasks = tasks;
this.unfilteredTasks=tasks;
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return tasks.size();
}
@Override
public Task getItem(int position) {
// TODO Auto-generated method stub
return (null==tasks)?null:tasks.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TaskListItem tli;
if(null==convertView){
tli=(TaskListItem)View.inflate(context, R.layout.task_list_item,null);
}else
{
tli=(TaskListItem)convertView;
}
tli.setTask(tasks.get(position));
return tli;
}
public void forceReload() {
notifyDataSetChanged();
}
public void toggleTaskCompleteAtPosition(int position) {
Task t=tasks.get(position);
t.toggleComplete();
notifyDataSetChanged();
}
public Long[] removeCompletedTasks() {
ArrayList<Task> completedTasks=new ArrayList<Task>();
ArrayList<Long> completedIds=new ArrayList<Long>();
for(Task task : tasks){
if(task.isComplete()){
completedIds.add(task.getId());
completedTasks.add(task);
}
}
tasks.removeAll(completedTasks);
notifyDataSetChanged();
return completedIds.toArray(new Long[]{});
}
public void filterTasksByLocation(Location latestLocation, long locationFilterDistance) {
filterTasks=new ArrayList<Task>();
for(Task task:tasks){
if(task.hasLocation() && taskIsWithinGeofence(task,latestLocation,locationFilterDistance)){
filterTasks.add(task);//chking all tasks with location tasks
Toast.makeText(context, task.getName(), Toast.LENGTH_LONG).show();//displaying name of the tasks(as per location)
}
}
tasks=filterTasks;
notifyDataSetChanged();
}
private boolean taskIsWithinGeofence(Task task, Location latestLocation,
long locationFilterDistance) {
float[] distanceArray=new float[1];
Location.distanceBetween(
task.getLatitude(),
task.getLongitude(),
latestLocation.getLatitude(),
latestLocation.getLongitude(),
distanceArray);
return (distanceArray[0]<locationFilterDistance);
}
public void removeLocationFilter() {
tasks=unfilteredTasks;
notifyDataSetChanged();
}
}
还有一个问题是应用程序没有在后台运行。位置只有在我启动应用程序时才会更新。感谢您的帮助...