小编Jam*_*ner的帖子

Android - 在设置同步适配器时启用自动同步

我正在编写一个使用同步适配器来同步数据的应用程序.

我已经阅读了文档,我很确定我理解它是如何工作的.我的大部分同步适配器都是按照Udi Cohen写的伟大指南来完成的.

但是,我确实有一个问题似乎无法解决,而且在安装我的应用程序时会自动启用同步.

当我的应用程序运行时,它会创建一个同步适配器和一个帐户,完成您希望它完成的所有工作,这很棒.但是,如果我转到"设置">"帐户">"我的应用程序",则同步已关闭.无论如何我可以让它自动启用吗?

帐户>'我的应用程序'的屏幕截图

设置同步适配器时,我的代码如下所示:

if(accountManager.addAccountExplicitly(account,null,null))
{
    // Inform the system that this account supports sync
    ContentResolver.setIsSyncable(account, CONTENT_AUTHORITY, 1);

    // Inform the system that this account is eligible for auto sync
    ContentResolver.setSyncAutomatically(account, CONTENT_AUTHORITY, true);

    // Recommend a schedule for auto sync
    ContentResolver.addPeriodicSync(account, CONTENT_AUTHORITY, new Bundle(), SYNC_FREQUENCY);

    newAccount = true;
}
Run Code Online (Sandbox Code Playgroud)

通过阅读同步适配器,我认为这ContentResolver.setSyncAutomatically()是用于自动启用同步的正确方法.

我也试过设置,ContentResolver.setMasterSyncAutomatically(true);但似乎没有任何影响.

android.permission.WRITE_SYNC_SETTINGS在AndroidManifest中声明了权限,所以这不是问题所在.我的项目中也有同步服务和xml文件.我在下面附上了这些代码.

还有其他人有过类似的问题吗?这可能是权限问题吗?如果有人有任何建议我会很乐意尝试.谢谢.

AndroidManifest.xml中

<service
    android:name=".syncadapter.CalendarSyncService"
    android:exported="true"
    android:process=":sync">
    <intent-filter>
        <action android:name="android.content.SyncAdapter"/>
    </intent-filter>
    <meta-data
        android:name="android.content.SyncAdapter"
        android:resource="@xml/calendar_syncadapter"/>
</service>
Run Code Online (Sandbox Code Playgroud)

syncadapter.xml

<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android" …
Run Code Online (Sandbox Code Playgroud)

java android android-syncadapter

5
推荐指数
1
解决办法
3846
查看次数

File.WriteAllText 正在根据 Git 更改我的文件的开头

我有一个非常简单的 C# 程序,它迭代多个文件并替换所有文件中的字符串。

然而,当我使用 Git 比较这些文件时,它突出显示了我所有文件的更改。

我的 C# 代码是:

string[] files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);

foreach (string file in files)
{
    string fileText = File.ReadAllText(fileName, Encoding.UTF8);
    string newText = fileText.Replace("hello", "goodbye");
    File.WriteAllText(fileName, newText, Encoding.UTF8);
}
Run Code Online (Sandbox Code Playgroud)

就我而言,这看起来不错。但是,当我运行该程序并git status在存储库上执行时,我发现每个文件都有差异。

使用 Github Desktop 或 SourceTree 等程序会带来以下变化:

Github 桌面

Github 桌面截图

源树

源树截图

感谢您提供任何人可能有的提示或想法。他们非常感激。:)

c# git

4
推荐指数
1
解决办法
476
查看次数

标签 统计

android ×1

android-syncadapter ×1

c# ×1

git ×1

java ×1