Android 检查设备上次连接到internet的时间

Android 检查设备上次连接到internet的时间,android,connection,periodic-task,Android,Connection,Periodic Task,我正在开发一款android应用程序,我想检查一下,自从上次该设备连接到互联网以来,是否已经超过10天了。我已经注册了一个网络更改接收器,以便获得连接更改,但我不知道如何检查自上次以来已更改了多少天。这是我的网络更改代码 IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); receiver = new NetwaorkChangeReceiver(); this.registerRece

我正在开发一款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;
        }
    }
}               

设置警报。Android
AlarmManager
由系统处理。创建一个
服务
,并根据服务意图制作一个
挂起内容
。使用该
pendingent
触发您的警报

 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");

}
触发报警管理器后,将启动
服务。你可以检查它的拍摄时间限制,如5分钟


查看以了解更多信息

你可以为你的应用程序添加上一次互联网连接的时间戳,并检查它是否超过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;

您是否在Androidmanifest中添加了权限???