使用SharedPreferences保存时间戳返回相同的值

时间:2014-08-20 23:38:44

标签: android calendar sharedpreferences

我正在尝试记录用户关闭/启动手机,启用/禁用飞行模式以及连接/断开数据连接的时间。

我使用SharedPreferences来存储所有这些时间,使用Calendar.getInstance()。getTime()。toString()

问题是,当我将设备置于飞机模式@(14:21:04),然后关闭设备@(14:24:08),然后重启@(14:30:06),然后禁用飞行模式@(14:34:04),日志输出所有事件的完全相同的时间,即使我间隔至少3分钟。

这是输出:

已发现设备。数据连接在2014年8月20日星期三14:21:04 PDT丢失

已发现设备。设备已于2014年8月20日星期三14:21:04 PDT关闭。

已发现设备。飞行模式于2014年8月20日星期三14:21:04 PDT启用

任何想法为什么?附:这不是为了恶意/坏意图:)

public class BootReceiver extends BroadcastReceiver {
private ProjectDebug LOGCAT = new ProjectDebug();


   @Override
   public void onReceive(final Context context, Intent intent) {
         SharedPreferences GETT_VALUE = context.getSharedPreferences("TIME_SAVER", Context.MODE_PRIVATE);
         SharedPreferences.Editor EDIT_VALUE = GETT_VALUE.edit();  

         String onReceive = "Boot Receiver",
                TimeStamp = Calendar.getInstance().getTime().toString(), 

                GetShutDownTime = GETT_VALUE.getString("SHUTDOWN_TIME", ""),
                GetAirPlaneTime = GETT_VALUE.getString("AIRPLANE_TIME", ""),
                GetDataConnTime = GETT_VALUE.getString("DATACONN_TIME", "");

         Editor SetShutDownTime = EDIT_VALUE.putString("SHUTDOWN_TIME", TimeStamp),
                SetAirPlaneTime = EDIT_VALUE.putString("AIRPLANE_TIME", TimeStamp),
                SetDataConnTime = EDIT_VALUE.putString("DATACONN_TIME", TimeStamp);


     /** @DEVICE_SHUTDOWN_EVENT */
         if (Intent.ACTION_SHUTDOWN.equalsIgnoreCase(intent.getAction())) { 

                      LOGCAT.DEBUG(onReceive, "Shutting Down Device");           
                      SetShutDownTime.commit();
         }

     /** @DEVICE_STARTUP_EVENT */
         if (Intent.ACTION_BOOT_COMPLETED.equalsIgnoreCase(intent.getAction())) {

                      LOGCAT.DEBUG(onReceive, "Device Start Up Completed");
                      POST_DEVICE_STATE("Restart Event", "Device has been discovered. Device was powered off on " + GetShutDownTime, context);                                                         
         }

      /**@AIRPLANE_MODE_TOGGLE */
         if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equalsIgnoreCase(intent.getAction().intern())) {   

                      boolean isEnabled = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1;
                      LOGCAT.DEBUG(onReceive, "Airplane Mode is turned " + (isEnabled ? "On" : "Off"));

                      if (isEnabled == true )   SetAirPlaneTime.commit();                                          
                      if (isEnabled == false)   POST_DEVICE_STATE("Airplane Event", "Device has been discovered. Airplane mode was enabled on " + GetAirPlaneTime, context);                                                            
         }            // Airplane mode was enabled on Wed Aug 13 16:44:54 PDT 2014



      /**@ESTABLISHED_CONNECTION */
         if (ConnectivityManager.CONNECTIVITY_ACTION.equalsIgnoreCase(intent.getAction())) {   

                      LOGCAT.DEBUG(onReceive, "Data Connection is " + (isInternetAvailable(context) ? "Established" : "Lost"));               
                      if (   isInternetAvailable(context))    POST_DEVICE_STATE("Connection Event", "Device has been discovered. Data connection was lost on " + GetDataConnTime, context);                                   
                      if ( ! isInternetAvailable(context))    SetDataConnTime.commit();                   
         }
   }


    /**
     * 
     * @param context
     * @param CURRENT_STATE
     * latlong must have some value in it for url to work
     */
 private void POST_DEVICE_STATE(final String Utility, final String CURRENT_STATE, final Context context) {

    AsyncTask<Void, Void, Void> LOG_DEVICE_STATE = new AsyncTask<Void, Void, Void>() { 

        SharedPreferences  Pref = context.getSharedPreferences("loginPrefs", Context.MODE_PRIVATE);

        String Tag = "POST_DEVICE_STATE",
               Username = (Pref.getString("username", USERNAME)),
               Password = (Pref.getString("password", PASSWORD)),
               STRIP_CURRENT_STATE = CURRENT_STATE.replace(" ", "+"),
               URLlink="https://www.something.com/?type=json&uid="+Username+"&pwd="+Password+"&network_provider="+"&reverse_geocode="+STRIP_CURRENT_STATE;


                @Override
                protected Void doInBackground(Void... params) {
                    Looper.getMainLooper();
                    LOGCAT.DEBUG(Tag, CURRENT_STATE);                           

                    final Handler handler = new Handler(Looper.getMainLooper());
                    handler.postDelayed(new Runnable() {

                         @Override
                         public void run() {    

                            if ( isInternetAvailable(context)) {

                                     try {  LOGCAT.DEBUG(Tag, "Posting " + Utility + " State to Server");
                                            JSONObject sendlocation = JSONfunction.getJSONfromURL(URLlink,context); 
                                            sendlocation.getString("result");

                                     } catch (Exception e) {                 
                                            e.printStackTrace();
                                     }



                                     try {  LOGCAT.DEBUG("LOG_DEVICE_STATE", "Getting GPS Location");
                                            //GPS_Listener gps = new GPS_Listener(context);
                                            //gps.GetMyLocation(context);       

                                     } catch (Exception e) {
                                            e.printStackTrace();
                                     }

                            } 

                            if (! isInternetAvailable(context)) {
                                     LOGCAT.DEBUG(Tag, "No Internet Detected While posting a " + Utility + ". Trying again in 10 seconds.");
                                     POST_DEVICE_STATE(Utility, CURRENT_STATE, context);                                                                 
                            }  
                        }                                               
                    }, 10000); //Execute code after 10 Sec   
                    return null;        
                }               
    }; LOG_DEVICE_STATE.execute();  
 }  


   private boolean isInternetAvailable(Context context) {

        ConnectivityManager Connection = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo  Internet = Connection.getActiveNetworkInfo();

        if (Internet != null && Internet.isConnected()) { // IF INTERNET IS THERE//
            return true;

        } else {

            return false;
        }          
   }
}

1 个答案:

答案 0 :(得分:0)

即使在执行条件之前,您也正在写入共享首选项。 然后相应地进行更改。 只有在可以解决问题的条件下才会插入更改。