Beispiel #1
0
 public void unregisteralarm(WeatherInfo wInfo, long time) {
   WeatherInfo weatherInfo = new WeatherInfo();
   weatherInfo.setCityname(wInfo.getCityname());
   weatherInfo.setDate(wInfo.getDate());
   weatherInfo.setPubdate(wInfo.getPubdate());
   Intent intent = new Intent("com.cdji.MY_ALARM");
   //		intent.setClass(NewAlarm.this, AlarmSever.class);
   Bundle bundle = new Bundle();
   bundle.putSerializable("WeatherInfo", weatherInfo);
   intent.putExtras(bundle);
   PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
   AlarmManager aManager;
   aManager = (AlarmManager) getSystemService(ALARM_SERVICE);
   aManager.cancel(pendingIntent);
   Log.i(
       "TAG",
       "取消了一个闹钟事件" + weatherInfo.getCityname() + weatherInfo.getDate() + weatherInfo.getPubdate());
 }
Beispiel #2
0
  // 初始化本activity数据
  private void initdate() {
    // TODO Auto-generated method stub
    DataBaseHelper dbhelper = new DataBaseHelper(this);
    SQLiteDatabase searchdb = dbhelper.getReadableDatabase();
    Cursor cursor =
        searchdb.rawQuery(
            "select * from seachedcity where cityname=? and isalarm=? order by date asc;",
            new String[] {cityname, "1"});
    list.clear();
    count = 0;
    while (cursor.moveToNext()) {
      WeatherInfo wInfo = new WeatherInfo();
      wInfo.setCityname(cursor.getString(2));
      wInfo.setIsalarm(cursor.getString(11));
      wInfo.setIsrecode(cursor.getString(10));
      wInfo.setIssubscribe(cursor.getString(12));
      // 时间转换
      long date = Long.valueOf(cursor.getString(3));
      long current = System.currentTimeMillis();
      if (date < current) {
        // 删除过时的提醒
        searchdb.execSQL(
            "delete from seachedcity where cityname=? and date=?",
            new String[] {cursor.getString(2), cursor.getString(3)});
        // 删除过时提醒后,为空?取消订阅
        if (cursor.isLast()) {
          searchdb.execSQL(
              "delete from seachedcity where cityname=? and subscribe=? ;",
              new String[] {cursor.getString(2), "1"});
        }

        continue;
      }
      SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

      System.out.println(date);
      String string = sFormat.format(date);
      String[] string2 = string.split(" ");
      System.out.println(string2[0] + " " + string2[1]);
      wInfo.setDate(string2[0]);
      wInfo.setPubdate(string2[1]);

      list.add(wInfo);
      count++;
    }
    cursor.close();
    searchdb.close();
    if (list.isEmpty()) {
      System.out.println("没有提醒信息");
      return;
    }
    System.out.println("有提醒信息");
    dateistAdapter =
        new CitylistAdapter<WeatherInfo>(this, list) {

          @Override
          public View init(Context context, final int position, WeatherInfo item) {
            // TODO Auto-generated method stub
            final WeatherInfo weatherInfo = item;
            LayoutInflater lInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            View view = lInflater.inflate(R.layout.alarm_listview_item, null);
            TextView daTextView = (TextView) view.findViewById(R.id.weatherdate);
            TextView timeTextView = (TextView) view.findViewById(R.id.weathertime);
            TextView weekTextView = (TextView) view.findViewById(R.id.weekday);
            Button deletebtn = (Button) view.findViewById(R.id.delete_city_btn);

            daTextView.setText(item.getDate());
            timeTextView.setText(item.getPubdate());
            weekTextView.setText(item.getweek());

            deletebtn.setOnClickListener(
                new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                    // TODO Auto-generated method stub
                    SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    long time = 0;

                    try {
                      time =
                          sFormat
                              .parse(weatherInfo.getDate() + " " + weatherInfo.getPubdate())
                              .getTime();
                    } catch (ParseException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                    }

                    DataBaseHelper dbhelper = new DataBaseHelper(AlarmSet.this);
                    SQLiteDatabase searchdb = dbhelper.getReadableDatabase();
                    searchdb.execSQL(
                        "delete from seachedcity where cityname=? and date=?",
                        new String[] {weatherInfo.getCityname(), String.valueOf(time)});
                    list.remove(position);
                    dateistAdapter.notifyDataSetInvalidated();
                    unregisteralarm(weatherInfo, time);
                    count--;
                    if (list.isEmpty()) {
                      // 检测是否删除了所有提醒,当提醒为空是,取消标志位。
                      searchdb.execSQL(
                          "delete from seachedcity where cityname=? and subscribe=? ;",
                          new String[] {weatherInfo.getCityname(), "1"});
                    }
                    searchdb.close();
                  }
                });

            return view;
          }
        };
    listView.setAdapter(dateistAdapter);
  }