1

我正在制作一个应用程序。作为位置任务提醒。当用户到达特定位置时,如果有与该位置相关的任务,他/她将获得该任务的其余部分。

我已经完成了一半以上的应用程序。当用户在该位置时,我可以显示任务。但它是手动的。用户必须手动打开 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();
}

}

还有一个问题是应用程序没有在后台运行。位置只有在我启动应用程序时才会更新。感谢您的帮助...

4

1 回答 1

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);

仅供参考,不需要广播接收器或服务(实际上我们使用的是 location_service,它将根据传递给 locationmanager 的参数来做所有事情)。

祝你好运

于 2012-10-31T11:19:48.917 回答