如何在安卓系统上禁用日期时间设置?

6
我们有一个应用程序,它会记录用户触发事件的正确日期和时间。 我们不希望用户将日期和时间更改为过去的时间。 如何在Android系统级别上禁用日期和时间设置?
1个回答

10

即使你能找到某种方法来做到这一点,这也不是你想做的事情。更好的解决方案是监听 ACTION_TIMEZONE_CHANGED、ACTION_TIME_CHANGED 和 ACTION_DATE_CHANGED 事件,然后相应地更改之前的时间。实际上,这非常容易做到,如果需要帮助,我可以提供示例代码。

TimeChanged.java

package com.example.stackoverflow17462606;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class TimeChanged extends BroadcastReceiver {
    public TimeChanged() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // Do whatever changes you need here
        // you can check the updated time using Calendar c = Calendar.getInstance();
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.stackoverflow17462606"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver
            android:name="com.example.stackoverflow17462606.TimeChanged"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.TIMEZONE_CHANGED"/>
                <action android:name="android.intent.action.TIME_SET"/>
                <action android:name="android.intent.action.DATE_CHANGED"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>
请记住,只有在您已经在设备上启动过应用程序的情况下,此代码才会执行(以防止应用程序在安装后自行运行)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接