繁体   English   中英

检查设备上次何时连接到互联网

[英]Check when a device last time connected to the internet

我正在开发一个Android应用程序,我想检查距该设备上次连接互联网以来是否已经超过10天。 我已经注册了一个网络更改接收器,以便获得连接更改,但是我不知道如何检查自上次以来已有多少天。 这是我的网络更改代码

IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
receiver = new NetwaorkChangeReceiver();
this.registerReceiver(receiver, filter);

public class NetworkChangeReceiver extends BroadcastReceiver {
private static boolean deviceConnected;
    private static final String LOG_TAG = NetworkChangeReceiver.class.getSimpleName();
    @Override
    public void onReceive(final Context context, final Intent intent) 
    {
        final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
        final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (wifi.isAvailable() || mobile.isAvailable()) 
        {
            deviceConnected=true;
        }else{
            deviceConnected=false;
        }
    }
}               

您可以在应用程序的最后一次互联网连接上添加时间戳,并检查该时间戳是否已超过10天。

mSharedPrefs = context.getSharedPreferences("preferences_filename", Context.MODE_PRIVATE);
mSharedPrefs .putLong("timestamp", System.currentTimeMillis()).apply();

并检查使用:

TEN_DAYS = 10 * 24 * 60 * 60 * 1000;
System.currentTimeMillis() - sharedPrefsValue > TEN_DAYS;

设置闹钟。 Android AlarmManager由系统处理。 创建一个Service并使用该服务意图创建一个PendingIntent 使用该PendingIntent触发警报。

 private void setAlarmForTenDays(Context context) {
     PendingIntent pendingIntentAutoBackupService = PendingIntent.getService(context,0,new Intent(context, MyService.class),0);
     AlarmManager manager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
     //long interval = 1000*60*60*24*10;
      long interval = AlarmManager.INTERVAL_DAY*10;
     manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntentAutoBackupService);
    Log.d("Alarm", " Alarm created");

}

触发警报管理器后,您的Service将启动。 您可以使用5分钟的拍摄时间限制进行​​检查。

选中计划重复警报以了解更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM