2011-09-02 139 views
0

我有一個android應用程序,它會記錄每x分鐘的數據。鬧鐘響起的時間取決於設置頁面(10或30分鐘)。當警報進入初始屏幕時,警報首先被激活。我的問題是,當我在手機上使用它時似乎沒有被調用,所以我希望有人能告訴我,如果我正確使用該功能。如果我是否正確使用警報?

if (prefs.getBoolean("backgroundupdates", true)) { 
     Intent setAlarm = new Intent(Splash.this, 
       UpdateLocation.class); 
     PendingIntent pendingIntent = PendingIntent.getService(
       Splash.this, 0, setAlarm, 0); 
     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
     Calendar calendar = Calendar.getInstance(); 
     SimpleDateFormat sdf = new SimpleDateFormat(
       "yyyy-MM-dd hh:mm:ssaa"); 
     History.addHistory(sdf.format(calendar.getTime()), "App Started", ""); 
     calendar.setTimeInMillis(System.currentTimeMillis()); 

     int UPDATE_TIME = prefs.getInt("Update_time", 30); 
     calendar.add(Calendar.MINUTE, UPDATE_TIME); 
     alarmManager.set(AlarmManager.RTC_WAKEUP, 
       calendar.getTimeInMillis(), pendingIntent); 


    } 

此背景檢查更新並設置一個報警UPDATE_TIME分鐘(默認30)

在我UpdateLocation.class

我有這樣的在底部

Intent setAlarm = new Intent(UpdateLocation.this, 
        UpdateLocation.class); 
      PendingIntent pendingIntent = PendingIntent.getService(
        UpdateLocation.this, 0, setAlarm, 0); 
      AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
      Calendar calendar = Calendar.getInstance(); 
      calendar.setTimeInMillis(System.currentTimeMillis()); 
      int UPDATE_TIME = prefs.getInt("Update_time", 30); 
      calendar.add(Calendar.MINUTE, UPDATE_TIME); 
      alarmManager.set(AlarmManager.RTC_WAKEUP, 
        calendar.getTimeInMillis(), pendingIntent); 

這是如此該警報將在UPDATE_TIME分鐘中再次自行調用。我這樣做的原因是因爲UPDATE_TIME可能會改變(取決於用戶的喜好)

謝謝!這裏

編輯是我UpdateLocation.class

public class UpdateLocation extends IntentService { 
    public static final String id = ""; 

    public UpdateLocation() { 
     super("UpdateLocation"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 

     SharedPreferences prefs = getSharedPreferences("Settings", 0); 
     final String id = prefs.getString("ID", ""); 
     if (prefs.getBoolean("backgroundupdates", true)) { 
      HttpParams httpParams = new BasicHttpParams(); 
      // 30seconds and it stops 
      HttpConnectionParams.setConnectionTimeout(httpParams, 30000); 
      HttpConnectionParams.setSoTimeout(httpParams, 30000); 
      DefaultHttpClient httpclient = new DefaultHttpClient(httpParams); 
      HttpPost httpost = new HttpPost(
        "http://iphone-radar.com/gps/gps_locations"); 

      JSONObject holder = new JSONObject(); 

      try { 
       holder.put("id", id); 
       LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
       Criteria criteria = new Criteria(); 
       criteria.setAccuracy(Criteria.ACCURACY_FINE); 
       String bestProvider = locationManager.getBestProvider(criteria, 
         false); 
       LocationListener loc_listener = new LocationListener() { 

        @Override 
        public void onStatusChanged(String provider, int status, 
          Bundle extras) { 
         // TODO Auto-generated method stub 

        } 

        @Override 
        public void onProviderEnabled(String provider) { 
         // TODO Auto-generated method stub 

        } 

        @Override 
        public void onProviderDisabled(String provider) { 
         // TODO Auto-generated method stub 

        } 

        @Override 
        public void onLocationChanged(Location location) { 
         // TODO Auto-generated method stub 

        } 
       }; 
       try { 
        Looper.prepare(); 
        locationManager.requestLocationUpdates(bestProvider, 0, 0, 
          loc_listener); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       Location location = locationManager 
         .getLastKnownLocation(bestProvider); 
       Calendar c = Calendar.getInstance(); 
       SimpleDateFormat sdf = new SimpleDateFormat(
         "hh:mmaa MM/dd/yyyy"); 
       holder.put("time", sdf.format(c.getTime())); 
       holder.put("time_since_epoch", System.currentTimeMillis()/1000); 
       try { 
        holder.put("lat", location.getLatitude()); 
        holder.put("lon", location.getLongitude()); 
       } catch (NullPointerException e) { 
        try { 
         holder.put("lat", -1.0); 
         holder.put("lon", -1.0); 
        } catch (JSONException e1) { 
         // TODO Auto-generated catch block 
         e1.printStackTrace(); 
        } 
       } 

       StringEntity se = new StringEntity(holder.toString()); 
       httpost.setEntity(se); 
       httpost.setHeader("Accept", "application/json"); 
       httpost.setHeader("Content-type", "application/json"); 

       ResponseHandler responseHandler = new BasicResponseHandler(); 
       String response = httpclient.execute(httpost, responseHandler); 
       org.json.JSONObject obj; 
       SimpleDateFormat sdf2 = new SimpleDateFormat(
         "yyyy-MM-dd hh:mm:ssaa"); 
       try{ 
       History.addHistory(sdf2.format(c.getTime()), "GPS", 
         "Latitude: " + location.getLatitude() + "\n" 
           + "Longitude: " + location.getLongitude());} 
       catch(NullPointerException e){ 
        History.addHistory(sdf2.format(c.getTime()), "GPS", 
          "Latitude: " + "No Signal" + "\n" 
            + "Longitude: " + "No Signal"); 
       } 
       obj = new org.json.JSONObject(response); 
       Intent setAlarm = new Intent(UpdateLocation.this, 
         UpdateLocation.class); 
       PendingIntent pendingIntent = PendingIntent.getService(
         UpdateLocation.this, 0, setAlarm, 0); 
       AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
       Calendar calendar = Calendar.getInstance(); 
       calendar.setTimeInMillis(System.currentTimeMillis()); 
       int UPDATE_TIME = prefs.getInt("Update_time", 30); 
       calendar.add(Calendar.MINUTE, UPDATE_TIME); 
       alarmManager.set(AlarmManager.RTC_WAKEUP, 
         calendar.getTimeInMillis(), pendingIntent); 

      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (UnsupportedEncodingException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 
    } 

} 

注意,我沒有廣播接收器,我不太知道如何使用它,因此任何例子將是非常有益的。

回答

0

你可能會發布所有的UpdateLocation.class?它需要延長BroadcastReceiver,以便可能是您的問題所在。

至於你的鬧鐘,如果它被設置爲每隔xx分鐘沒有結束時關閉,你最好使用alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), UPDATE_TIME * 60000, pendingIntent);定期重置鬧鐘。


編輯1

關於把的PendingIntent成方便地訪問靜態函數,它可以去是這樣的:

public static PendingIntent getPendingIntent(int update_time) { 
    Intent setAlarm = new Intent(this.getBaseContext(), UpdateLocation.class); 
    PendingIntent pendingIntent = PendingIntent.getService(this.getBaseContext(), 
                  0, setAlarm, 0) 
    return pendingIntent; 
} 

然後你就可以用alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), UPDATE_TIME * 60000, MyClass.getPendingIntent());讓你的鬧鐘

+0

我現在就更新它。 setRepeating方法是否每次重複檢查UPDATE_TIME?因爲用戶可以隨時更改UPDATE_TIME的值。 –

+0

它的重複值保持不變,但可以隨時更新。調用'alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),UPDATE_TIME * 60000,pendingIntent);'具有相同的PendingIntent將自動取消現有的警報並重新添加新的時間延遲。 –

+0

至於BroadcastReceiver,我使用[本教程](http://justcallmebrian.com/?p=129)來幫助我。請記住在Android Manifest中添加額外的''標籤。 –