从意图android获取错误的值

时间:2015-05-22 15:52:06

标签: android android-intent alert

我是Android的新手,我正在开发基于位置的提醒的第一个应用程序。我正在添加地点,日期和时间的任务,当我到达时,我应该得到通知。我正在为任务正确地收到警报。问题是当为任务生成警报时,单击通知时应在活动中显示警报。我没有得到警报任务的值..

从下面的代码我将任务值传递给Alertview活动。

代码

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活动: -

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

}
}

为警报添加的任务是: - 任务名称: - Matunga Loc:-Matunga Labourcamp 日期: - 2015年5月22日 时间: - 22:16

当警报被解雇时,我得到以下值: - 任务名称: - Matunga Loc:-SIES学院,Sion 日期: - 2015年5月22日 时间: - 11:11

第二次添加提醒时: - 任务名称: - Sion Loc:-Transist camp 日期: - 2015年5月22日 时间: - 22:19

使用以下值触发警报: - 任务名称: - Matunga Loc:-SIES学院,Sion 日期: - 2015年5月22日 时间: - 11:11

M在传递意图之前添加的Toast中获取正确的值。

if (timeRange && locRange) {
                Toast.makeText(getApplicationContext(),"Your Task is " + task.toString(),Toast.LENGTH_LONG).show();

2 个答案:

答案 0 :(得分:2)

由于您的PendingIntent已经存在,因此您的意图数据不会更新。要更新它,您需要PendingIntent标记为PendingIntent.FLAG_UPDATE_CURRENT

 PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

根据PendingIntent doc。

  

public static final int FLAG_UPDATE_CURRENT

     

表示如果描述的PendingIntent已存在的标志,   然后保留它,但用这个新的东西替换它的额外数据   意图。

答案 1 :(得分:0)

使用PendingIntent.FLAG_UPDATE_CURRENT仅是解决方案的一部分。提供唯一的requestCode也很重要:

final PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);