Android 检测应用程序是否由同步适配器启动

Android 检测应用程序是否由同步适配器启动,android,android-syncadapter,Android,Android Syncadapter,我正在使用android同步适配器。当系统启动同步时,我的应用程序将启动,或者调用onCreate()方法 在我的应用程序中,我继承应用程序类并在onCreate()函数中编写一些自定义代码。如果同步适配器启动应用程序,我不希望执行这些自定义代码 我想知道如何检测应用程序是否由同步适配器启动?谢谢。检查清单文件中同步进程的进程名称(“我的案例:sync”) 在Application.onCreate调用上述代码以检测当前进程是否同步 public class MyApplication exte

我正在使用android同步适配器。当系统启动同步时,我的应用程序将启动,或者调用onCreate()方法

在我的应用程序中,我继承应用程序类并在onCreate()函数中编写一些自定义代码。如果同步适配器启动应用程序,我不希望执行这些自定义代码


我想知道如何检测应用程序是否由同步适配器启动?谢谢。

检查清单文件中同步进程的进程名称(“我的案例:sync”)

在Application.onCreate调用上述代码以检测当前进程是否同步

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        String processName = Helper.getCurrentProcessName(this);
        if (processName.endsWith(":sync")) {
            Log.d(TAG, ":sync detected");
            return;
        }
    }
}
public String getCurrentProcessName(Context context) {
    // Log.d(TAG, "getCurrentProcessName");
    int pid = android.os.Process.myPid();
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses())
    {
        // Log.d(TAG, processInfo.processName);
        if (processInfo.pid == pid)
            return processInfo.processName;
    }
    return "";
}
public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        String processName = Helper.getCurrentProcessName(this);
        if (processName.endsWith(":sync")) {
            Log.d(TAG, ":sync detected");
            return;
        }
    }
}