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

Android 使用SharedReferences保存时间戳返回相同的值,android,calendar,sharedpreferences,Android,Calendar,Sharedpreferences,我试图记录用户关闭/启动手机、启用/禁用飞行模式以及连接/断开数据连接的时间 我使用Calendar.getInstance().getTime().toString()使用SharedReferences存储所有这些时间 问题是,当我将设备置于飞行模式@(14:21:04),然后关闭设备@(14:24:08),然后重新启动它@(14:30:06),然后禁用飞行模式@(14:34:04),所有事件的日志输出时间都完全相同,即使我将所有事件间隔至少3分钟 以下是输出: 已发现该设备。2014年8月

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

我使用Calendar.getInstance().getTime().toString()使用SharedReferences存储所有这些时间

问题是,当我将设备置于飞行模式@(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启用了飞机模式

你知道为什么吗?注意:这不是出于邪恶/恶意:)

公共类BootReceiver扩展了BroadcastReceiver{
private ProjectDebug LOGCAT=new ProjectDebug();
@凌驾
公共void onReceive(最终上下文、意图){
SharedReferences GETT\u VALUE=context.getSharedReferences(“节省时间”,context.MODE\u PRIVATE);
SharedReferences.Editor EDIT_VALUE=GETT_VALUE.EDIT();
字符串onReceive=“启动接收器”,
时间戳=Calendar.getInstance().getTime().toString(),
GetShutDownTime=GETT_VALUE.getString(“SHUTDOWN_TIME”,”),
GetAirPlaneTime=GETT_VALUE.getString(“飞机时间”,即“”),
GetDataConnTime=GETT_VALUE.getString(“DATACONN_TIME”,即“”);
编辑器SetShutDownTime=EDIT_VALUE.putString(“关机时间”,时间戳),
SetAirPlaneTime=EDIT_VALUE.putString(“飞机时间”,时间戳),
SetDataConnTime=编辑_值.putString(“DATACONN_时间”,时间戳);
/**@DEVICE\u SHUTDOWN\u事件*/
if(Intent.ACTION\u SHUTDOWN.equalsIgnoreCase(Intent.getAction()){
调试(onReceive,“关闭设备”);
setshutdown.commit();
}
/**@DEVICE\u启动\u事件*/
if(Intent.ACTION\u BOOT\u COMPLETED.equalsIgnoreCase(Intent.getAction())){
LOGCAT.DEBUG(onReceive,“设备启动完成”);
POST_设备_状态(“重新启动事件”,“设备已被发现。设备已在“+GetShutDownTime,上下文)”上关闭电源);
}
/**@飞机模式切换*/
如果(Intent.ACTION\u airpair\u MODE\u CHANGED.equalsIgnoreCase(Intent.getAction().intern()){
布尔值isEnabled=Settings.System.getInt(context.getContentResolver(),Settings.System.player_MODE_ON,0)==1;
LOGCAT.DEBUG(onReceive,“飞机模式打开”+(isEnabled?“打开”:“关闭”);
如果(isEnabled==true)SetAirPlaneTime.commit();
如果(isEnabled==false)处于设备状态(“飞机事件”,“设备已被发现。飞机模式已在“+GetAirPlaneTime,context”上启用);
}//2014年8月13日星期三16:44:54 PDT启用了飞机模式
/**@已建立的\u连接*/
if(ConnectivityManager.CONNECTIVITY_ACTION.equalsIgnoreCase(intent.getAction()){
调试(onReceive,“数据连接为”+(isInternetAvailable(上下文)?“已建立”:“丢失”);
如果(isInternetAvailable(context))处于设备状态(“连接事件”,“设备已被发现。数据连接在”+GetDataConnTime,context)上丢失);
如果(!isInternetAvailable(context))SetDataConnTime.commit();
}
}
/**
* 
*@param上下文
*@param当前状态
*latlong中必须有一些值,url才能工作
*/
私有void POST_设备_状态(最终字符串实用程序、最终字符串当前_状态、最终上下文){
AsyncTask日志\设备\状态=新建AsyncTask(){
SharedReferences Pref=context.getSharedReferences(“loginPrefs”,context.MODE\u PRIVATE);
String Tag=“POST\u DEVICE\u STATE”,
用户名=(Pref.getString(“用户名”,Username)),
Password=(Pref.getString(“Password”,Password)),
条带当前状态=当前状态。替换(“,“+”),
URL链接=”https://www.something.com/?type=json&uid=“+Username+”&pwd=“+Password+”&network_provider=“+”&reverse_geocode=“+STRIP_CURRENT_STATE;
@凌驾
受保护的Void doInBackground(Void…参数){
Looper.getMainLooper();
调试(标签、当前状态);
最终处理程序=新处理程序(Looper.getMainLooper());
handler.postDelayed(新的Runnable(){
@凌驾
public void run(){
如果(iInternetAvailable(上下文)){
尝试{LOGCAT.DEBUG(标记,“向服务器发布”+实用程序+“状态”);
JSONObject sendlocation=JSONfunction.getJSONfromURL(URL链接,上下文);
sendlocation.getString(“结果”);
}捕获(例外e){
e、 printStackTrace();
}
尝试{LOGCAT.DEBUG(“记录设备状态”,“获取GPS位置”);
//全球定位系统
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;
        }          
   }
}