vendredi 22 mai 2015

getting wrong values from intent android

I am new to android and I am developing my 1st application for location based reminders. I am adding tasks with locations, dates and times for which when I reach I should be notified. I'm getting the alert properly for the task. The problem is when the alert is generated for a task it should be displayed in an activity when I click the notification. I am not getting the values of the alerted task..

From the below code I'm passing the task values to the Alertview activity.

Code

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

            }

        }
    }
}

Alertview activity :-

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()); 
    //Toast.makeText(getApplicationContext(),"Your Task is " + task.toString(),Toast.LENGTH_LONG).show();

}
}

Aucun commentaire:

Enregistrer un commentaire