我在製作應用程序。作爲位置任務提醒。 當用戶到達特定位置時,如果有任務與該位置相關,他/她將獲得該任務的其餘部分。如何使用通知,播放位置
我已經完成了超過一半的應用程序。我可以顯示用戶在該位置時的任務,但它是手動的。用戶必須手動打開d應用程序。
我的問題是:如何在用戶到達特定位置時彈出該任務的Notifications
。這裏需要Broadcast Receiver
嗎?並且怎麼樣使用它作爲一個Service
這是我code.Its只爲顯示切換按鈕上一個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();
}
}
還的問題是,應用程序不運行background.location僅得到更新,當我啓動應用程序。 感謝NE有點幫助...
你知道使用CAPITAL字母相當於SHOUTING嗎? –
OP:你有標籤,所以沒有必要在問題主題中加入「android」 –
@ WebnetMobile.com:好的。錯過了我的編輯。 :-) –