0

我需要创建一个应用程序,我必须在其中显示当前访问该应用程序的用户总数。(类似于网页中的 web-stat,我们可以显示“Total Visitor:1000”)。

只要任何用户打开应用程序(在全球范围内),他应该会看到截至目前的总访客数。

是否有针对此要求的解决方案?

感谢任何帮助/建议。谢谢

--更多信息-- 有一个网站http://www.web-stat.net/是为网页做的。安卓应用需要类似的东西。

4

2 回答 2

1

你可以读到的两个例子:

http://www.flurry.com

http://www.google.com/analytics/

这两个都是 DataReporting 服务,提供 API 来导出您的数据,例如 JSON。主要目的是您的应用程序将使用统计信息发送到外部服务器(您可以在其中查看数据)。此外,您还可以连接到这些服务器并在您的设备上检索数据。

更新 - 本地存储

如果您只想要设备上的本地计数器来显示用户访问应用程序的频率,请查看SharedPreferences。SharedPreferences 使您能够将数据存储在您的设备上,并在您打开或关闭应用程序时再次检索该数据。

更新 - 全球 - 数据库

如果您只想要一个“全局”计数器。我建议您为存储当前访问者计数的数据库创建一个SQL 数据库,并在每次用户访问时递增它,并在应用程序启动时接收当前计数以显示它。如果您想跟踪当前正在使用应用程序的应用程序的不同用户,您可以为每个用户生成一个相同的 id,并在用户首次打开应用程序时将其存储在您的 SQL 数据库中并增加计数器。为此,您当然还需要一个存储 id 的表。当用户关闭应用程序时,您将需要再次连接到数据库并从表中删除他的 id 并减少计数器。

这可能是为每个用户获取唯一 ID 的一种方式:

public class Installation {
    private static String sID = null;
    private static final String INSTALLATION = "INSTALLATION";

    public synchronized static String id(Context context) {
        if (sID == null) {  
            File installation = new File(context.getFilesDir(), INSTALLATION);
            try {
                if (!installation.exists())
                    writeInstallationFile(installation);
                sID = readInstallationFile(installation);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return sID;
    }

    private static String readInstallationFile(File installation) throws IOException {
        RandomAccessFile f = new RandomAccessFile(installation, "r");
        byte[] bytes = new byte[(int) f.length()];
        f.readFully(bytes);
        f.close();
        return new String(bytes);
    }

    private static void writeInstallationFile(File installation) throws IOException {
        FileOutputStream out = new FileOutputStream(installation);
        String id = UUID.randomUUID().toString();
        out.write(id.getBytes());
        out.close();
    }
}

取自这里:Android 唯一 ID

于 2013-08-12T06:54:20.047 回答
0
  1. 扩展应用程序类。
  2. 创建一个可以增加和减少用户数量的网络方法。
  3. 使用 onCreate() 方法报告活跃用户(增加数量)。
  4. 使用 onTerminate() 方法报告非活动用户(减少数量)。
  5. 创建一个获取用户数量的方法。
于 2013-08-12T06:57:36.890 回答