2012-10-31 68 views
1

我在製作應用程序。作爲位置任務提醒。 當用戶到達特定位置時,如果有任務與該位置相關,他/她將獲得該任務的其餘部分。如何使用通知,播放位置

我已經完成了超過一半的應用程序。我可以顯示用戶在該位置時的任務,但它是手動的。用戶必須手動打開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有點幫助...

+2

你知道使用CAPITAL字母相當於SHOUTING嗎? –

+1

OP:你有標籤,所以沒有必要在問題主題中加入「android」 –

+0

@ WebnetMobile.com:好的。錯過了我的編輯。 :-) –

回答

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的參數來執行所有操作)。

祝你好運

+0

嘿thnks ..很有幫助 –

+0

嘿請chk我的編輯問題..thnks –

相關問題