2015-05-22 162 views
1

我正在開發一個基於位置的提醒應用程序,我在哪裏添加任務的位置和日期和時間,當我到達我應該被通知。
我正在使用序列化爲此我將任務存儲在文件中。我的問題是當我嘗試加載for循環中的任務文件來檢查警報,但它只檢查第一個添加的任務。我希望for循環應該完成所有任務。爲循環無法正常工作android

下面是for循環:

private void processTasks() { 

    List<Task> tasks = loadFiles(); 

    if(tasks == null || tasks.size() < 1){ 
    // Toast.makeText(getApplicationContext(),"No Tasks Found",Toast.LENGTH_LONG).show(); 
    } 
    // Toast.makeText(getApplication(),"Number of Tasks " + tasks.size(),Toast.LENGTH_LONG).show(); 

    for (Task task : tasks) { 

     if (tasks != null && task.alert) { 
      Log.i("Task is ",task.toString()); 
      Toast.makeText(getApplicationContext(),"Your Task is " + task.toString(),Toast.LENGTH_LONG).show(); 

      // if in time range 
      boolean timeRange = false; 

      // if in loc range 
      boolean locRange = false; 
      float dst = 0.0f; 
      if(lat > 0.0 && lon > 0.0) 
      { 
       float[] results = new float[1]; 
       Location.distanceBetween(lat, lon, task.getTaskLat(), task.getTaskLon(), results); 
       float distanceInMeters = results[0]; 
       dst = distanceInMeters; 
       Log.i("dist",""+distanceInMeters); 
       boolean isWithin5km = distanceInMeters < 5000; 
       locRange = isWithin5km; 
      } 
      Log.i("bool",""+locRange); 
      // Toast.makeText(this,"dist is " + dst ,Toast.LENGTH_LONG).show(); 

      if(dst < 5000){ 
       // Toast.makeText(getApplicationContext(),"Booooom",Toast.LENGTH_LONG).show(); 
       locRange = true; 
      } 
      else { 
       locRange = false; 
       // Toast.makeText(getApplicationContext(),"Faaalse",Toast.LENGTH_LONG).show(); 
      } 

      // Toast.makeText(getApplicationContext(),"Date is " + task.getDateString(),Toast.LENGTH_LONG).show(); 

      // format date 
      String datearr[] = task.getDateString().split("/"); 
      int day = Integer.parseInt(datearr[0]); 
      int month = Integer.parseInt(datearr[1]); 
      month--; 
      int year = Integer.parseInt(datearr[2].trim()); 

      // Toast.makeText(getApplicationContext(),"Date is " + datearr,Toast.LENGTH_LONG).show(); 

      // Toast.makeText(getApplicationContext(),"Time is " + task.getTimeString(),Toast.LENGTH_LONG).show(); 
      //format time 
      String timearr[] = task.getTimeString().split(":"); 

      if (timearr!=null) 
      { 
       Log.i("=>",timearr.toString()); 
       //Toast.makeText(getApplicationContext(),"Time is " + timearr.toString(),Toast.LENGTH_LONG).show(); 
      } 
      else { 
       Log.e("tme null","time null"); 
      // Toast.makeText(getApplicationContext(),"Time is Null",Toast.LENGTH_LONG).show(); 
      } 

      int hour = Integer.parseInt(timearr[0]); 
      int minute = Integer.parseInt(timearr[1]); 

      // month range 0-11 
      Calendar event = Calendar.getInstance(); 
      event.set(year, month, day, hour, minute); 

      Calendar now = Calendar.getInstance(); 

      Log.i("event", "" + event.toString()); 
      Log.i("now", "" + now.toString()); 

      int hourNow = now.get(Calendar.HOUR_OF_DAY); 
      int dNow = now.get(Calendar.DAY_OF_MONTH); 
      int mNow = now.get(Calendar.MONTH); 
      int yNow = now.get(Calendar.YEAR); 
      int minNow = now.get(Calendar.MINUTE); 

      Log.i("year", "" + yNow + " y " + year); 
      Log.i("mnth", "" + mNow + " m " + month); 
      Log.i("day", "" + dNow + " d " + day); 
      Log.i("minute","" + minNow + "min" + minute); 

      if (yNow == year && mNow == month && dNow == day) { 
       if (hourNow == hour && minNow == minute) { 
        timeRange = true; 
       } else { 
        Log.i("hour", "" + (hour - hourNow)); 
       } 
      } 
      // Toast.makeText(getApplicationContext(),"Values are :- " + day + " , " + month + " , " + year + " , " + hour + " , " + minute,Toast.LENGTH_LONG).show(); 

      // timeRange = true; 
      // Toast.makeText(getApplication(),"Time Range is " + timeRange,Toast.LENGTH_LONG).show(); 
      // Toast.makeText(getApplicationContext(),"Loc Range is " + locRange,Toast.LENGTH_LONG).show(); 

      if (timeRange && locRange) { 
       Toast.makeText(getApplicationContext(),"Your Task is " + task.toString(),Toast.LENGTH_LONG).show(); 
       Gson gson = new Gson(); 
       String JSON = gson.toJson(task); 
       Intent intent = new Intent(this, Alertview.class); 
       intent.putExtra("extra", JSON); 
       PendingIntent pIntent = PendingIntent.getActivity(this, 0, 
         intent, 0); 

       NotificationCompat.Builder builder = new NotificationCompat.Builder(
         getApplicationContext()); 
       builder.setSmallIcon(R.drawable.ic_launcher); 
       builder.setContentTitle("Alert !!!!"); 
       builder.setContentText("You are near your Point of Interest !!"); 
       builder.setContentIntent(pIntent); 
       builder.setAutoCancel(true); 
       NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
       manager.notify(99, builder.build()); 

       task.alert = false; 
       persistTask(task); 

      } else { 
       return; 
      } 

     } 
    } 
} 

AlertView.class:

public class Alertview extends Activity { 

Task task; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.alert_view); 

    Bundle extras = getIntent().getExtras(); 
    String JSON = extras.getString("extra"); 

    Gson gson = new Gson(); 
    task = gson.fromJson(JSON, Task.class); 

    TextView textView = (TextView)findViewById(R.id.textView5); 
    textView.setText(task.toString()); // <= NullPointerexception 
    //Toast.makeText(getApplicationContext(),"Your Task is " + task.toString(),Toast.LENGTH_LONG).show(); 

} 

}

+0

看看[this](https://developer.android.com/tools/debugging/debugging-studio.html)。 – moffeltje

+0

'tasks!= null' >>'task!= null' – moffeltje

回答

0

你可以嘗試更換:

} else { 
    return; 
} 

通過

} else { 
    continue; 
} 

因爲在我看來你的代碼在你的條件未被驗證時就停止處理。

+0

Thnx ..是啊你是對的我只是通過刪除其他部分來完成它.. m得到警報,但是它去了alertview活動m得到一些其他任務值..米不是沒有得到提醒的任務細節..我錯過了嗎?通過添加AlertView類來編輯帖子.. – user2205230