我正在制作一款应用。作为位置 - 任务提醒。 当用户到达特定位置时,如果某个任务与该位置相关,他/她将获得该任务的剩余部分。
我已经完成了超过一半的app.I可以在用户位于该位置时显示任务。但它是手动的。用户必须手动打开d app n chk。
我的问题是:当用户到达特定位置时,如何弹出该任务的Notifications
。这里需要Broadcast Receiver
吗?那么将它用作Service
这是我的代码。它仅用于在ToggleButton上的List.by clickig中显示位置任务,它仅向1kilometre(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();
}
}
另外一个问题是app没有在background.location中运行,只有在我启动应用程序时才会更新。 谢谢你的帮助...
答案 0 :(得分:0)
昨天我在我的应用程序中做了同样的事情(通知位置)这里是我的代码,可以帮助你..
NotificationManager nmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.icon = R.drawable.deal_image;
notification.tickerText = "new deal found";
notification.when = System.currentTimeMillis();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Log.d(TAG, "notifying deal is" + placename);
// After clicking on perticular notification i open my own activity like this
Intent intent = new Intent(getApplicationContext(),
ActDisplaylistdata.class);
intent.putExtra("lat", Currentlatitude);
intent.putExtra("long", Currentlongtitude);
Log.d(TAG, "checked value " + _checkedCategory.toString());
intent.putStringArrayListExtra("category", _checkedCategory);
PendingIntent pintent = PendingIntent.getActivity(
getApplicationContext(), i, intent, 0);
notification.setLatestEventInfo(getApplicationContext(), placename,
placename + " is " + distance + " away", pintent);
notification.number += 1;
nmanager.notify(i, notification);
并且FYI不需要广播接收器或服务(实际上我们正在使用location_service,它将根据传递给locationmanager的参数执行所有操作)。
祝你好运