android - 从 intent android 获取错误的值

标签 android android-intent alert

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

在下面的代码中,我将任务值传递给 Alertview Activity 。

代码

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

}
}

为警报添加的任务是:- 任务名称:- Matunga Loc :-马通加劳动营 日期:- 22/5/2015 时间:- 22:16

当我触发警报时,我得到以下值:- 任务名称:- Matunga Loc :-锡永SIES学院 日期:- 22/5/2015 时间:- 11:11

我第二次添加警报时:- 任务名称:- 锡安 Loc :-Transist阵营 日期:- 22/5/2015 时间:- 22:19

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

M 在通过 Intent 之前添加的 Toast 中获取正确的值。

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

最佳答案

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

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

根据 PendingIntent文档。

public static final int FLAG_UPDATE_CURRENT

Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent.

关于android - 从 intent android 获取错误的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30401040/

相关文章:

android - 从 map fragment 切换到另一个 fragment 并返回

java - Android - 每隔几秒改变球的颜色

javascript - PyQT4 Javascript 警报

Swift Firebase 身份验证 - 有关错误处理的两个问题(我不知道如何命名这些错误)

java - 计算Android中的手势距离

android - 未找到 setFragmentResult 和 setFragmentResultListener 包( Unresolved reference )

Android任务管理器源码

android - 在sqlite数据库中搜索

android - 无法覆盖使用相同 URI 拍摄的照片

页面加载后立即显示 Javascript 警报